Jump to content

Trying to get everything on 1 line


pwired
 Share

Recommended Posts

I have this code:

<?php
  $image = $page->images->get("name=logo.jpg");
  $image = $image->width(200);
  echo "<img src='$image->url'/>";
  ?>

I am trying to get those 3 lines on just 1 line like this:

<?php  echo "< . . . . everything on one line . . . . >"; ?>

But need a little help.

Link to comment
Share on other sites

It's not really getting any better with this, but how 'bout something like this? (Written in browser, not tested, might not work, you know the drill.)

<?php echo "<img src='" . $page->images->get("name=logo.jpg")->width(200)->url . "' />"; ?>
  • Like 2
Link to comment
Share on other sites

Guys I think it is almost working:

Have this error:

Parse Error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ';' (line 106 of /home/tibetischeknobla/public_html/site/templates/head.inc)

When I use this:

<li><?php echo "<img src='" . $pages->bxslider-images->images->get("name=hill.jpg")->width(200)->url . "' />"; ?></li>

Link to comment
Share on other sites

What if there's no such image? I think it's not about making it shorter but more fool proof.

<?php
if($page->images->count) {
     $image = $page->images->get("name=logo.jpg");
     if($image) echo "<img src='{$image->width(200)->url}'/>";
}
?>
  • Like 3
Link to comment
Share on other sites

I have this code:

<?php
  $image = $page->images->get("name=logo.jpg");
  $image = $image->width(200);
  echo "<img src='$image->url'/>";
  ?>
I am trying to get those 3 lines on just 1 line like this:

<?php  echo "< . . . . everything on one line . . . . >"; ?>

But need a little help.

Why in one line?

Link to comment
Share on other sites

Why in one line?

I only have 3 pictures in a bxslider and instead of repeating 3 times:

<?php
  $image = $page->images->get("name=logo.jpg");
  $image = $image->width(200);
  echo "<img src='$image->url'/>";
  ?>

I would like to have it 3 times on one line per image,

<?php  echo "< . . . . everything on one line . . . . >";

But now that I am using $pages instead of $page I have an error anyway

Parse Error: syntax error, unexpected '->'

Link to comment
Share on other sites

Can someone tell me how to change this $page code

<?php
  $image = $page->images->get("name=logo.jpg");
  $image = $image->width(200);
  echo "<img src='$image->url'/>";
  ?>

into code for using $pages instead $page ?

Link to comment
Share on other sites

Thanks LostKobrakai for your example,

<li>
<?php
$temp = $pages->get("/bxslider-pictures/");
$image = $temp->images->get("name=hill.jpg");
$image = $image->width(200);
echo "<img src='$image->url'/>";
?>
</li>

with it I am now a step further.

Link to comment
Share on other sites

Here is your one liner, but I wouldn't advise you to use it:

if($imageUrl = $pages->get("/bxslider-pictures/")->images->get("name=hill.jpg")->width(200)->url) echo "<img src='$imageUrl'/>";
  • Like 2
Link to comment
Share on other sites

@pwired

I am trying to get those 3 lines on just 1 line like this:

Is there a particular reason for this?

I'm with diogo on this one. I went through a phase of wanting to put everything as tersely as possible in PHP but I don't do that any more. I find single liners make it more difficult to see structure (where's the closing brace for the if statement in diogo's example above?), more difficult to read, more difficult to debug (especially if you single step with a debugger), more difficult to edit and more difficult to maintain than using multiple lines. Vertical space in a good text editor is almost unlimited - and easily navigated around. I'd rather pay the price of a few more lines of code to be able to instantly see the structure, closing braces and be able to step through the code line-by-line.

  • Like 3
Link to comment
Share on other sites

@netcarver - you are right about that.

It´s just that in case of only a few pictures in a slider e.g. 2 or 3, I thought one liners might be more quick to do it,

but maybe it is better to use the same way of coding for all cases.

I´m in a real mess anyway now. Tried to get bxslider in a processwire site

but fail miserably. Calling the script just before </body> or any other place

messes everything up. Maybe bxslider isn´t the most compatible one.

Lost too much time => Boss very angry.

Trying SlidesJS 3.0 or else I simply give up.

Link to comment
Share on other sites

I don't see why bxslider wouldn't work. The markup it requires is very simple, just a ul > li > img with free urls (it doesn't enforce that you specify only the folder like some old scripts did). In your case I would probably just put the images in the css folder and call them manually. I mean, if you know the name of the files is because you don't need the images to be dynamic, so why have them in a PW field?

$images = ["image1.jpg","image2.jpg","image3.jpg"];

echo "<ul>";
foreach ($images as $img) {
    echo "<li><img src='{$config->urls->templates}styles/slider_images/{$img}'/></li>";
}
echo "</ul>";
  • Like 3
Link to comment
Share on other sites

In your case I would probably just put the images in the css folder and call them manually. I mean, if you know the name of the files is because you don't need the images to be dynamic, so why have them in a PW field?

Thanks Diogo, for the code example and tip. Haven´t tried that possibility, so let´s hope.

Otherwise I will try tonight at home an empty processwire profile and start from scratch

with bxslider. Anyway, they have taken me off the website and now somebody else is

finishing it. I have pretty tough colleagues around me here, experienced coders.

Keep on dreaming of working for my self like you.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...