pwired Posted October 2, 2014 Share Posted October 2, 2014 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 More sharing options...
teppo Posted October 2, 2014 Share Posted October 2, 2014 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 . "' />"; ?> 2 Link to comment Share on other sites More sharing options...
pwired Posted October 2, 2014 Author Share Posted October 2, 2014 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 More sharing options...
Soma Posted October 2, 2014 Share Posted October 2, 2014 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}'/>"; } ?> 3 Link to comment Share on other sites More sharing options...
pwired Posted October 2, 2014 Author Share Posted October 2, 2014 I think I am using $pages in the wrong way. bxslider-images is an unpublished page with field images that holds picture hill.jpg Link to comment Share on other sites More sharing options...
diogo Posted October 2, 2014 Share Posted October 2, 2014 Does your hosting service charge per line? 7 Link to comment Share on other sites More sharing options...
blad Posted October 2, 2014 Share Posted October 2, 2014 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 More sharing options...
pwired Posted October 2, 2014 Author Share Posted October 2, 2014 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 More sharing options...
blad Posted October 2, 2014 Share Posted October 2, 2014 I don´t know what to tell you. I would use a foreach or an array with just several lines. Good luck 1 Link to comment Share on other sites More sharing options...
pwired Posted October 2, 2014 Author Share Posted October 2, 2014 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 More sharing options...
LostKobrakai Posted October 2, 2014 Share Posted October 2, 2014 <?php $site = $pages->get(SOMETHING); $image = $site->images->get("name=logo.jpg"); $image = $image->width(200); echo "<img src='$image->url'/>"; ?> But there's still no check whether the page or the image is acctually there. 1 Link to comment Share on other sites More sharing options...
pwired Posted October 2, 2014 Author Share Posted October 2, 2014 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 More sharing options...
diogo Posted October 2, 2014 Share Posted October 2, 2014 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'/>"; 2 Link to comment Share on other sites More sharing options...
netcarver Posted October 2, 2014 Share Posted October 2, 2014 @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. 3 Link to comment Share on other sites More sharing options...
pwired Posted October 2, 2014 Author Share Posted October 2, 2014 @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 More sharing options...
Martijn Geerts Posted October 2, 2014 Share Posted October 2, 2014 Haven't experienced any problems with bxslider so maybe there's an other root of the evil. When I have troubles with HTML soup I prefer to check the syntax with the validator of W3C. Lot's of time it's just an non closed element or such. 2 Link to comment Share on other sites More sharing options...
diogo Posted October 2, 2014 Share Posted October 2, 2014 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>"; 3 Link to comment Share on other sites More sharing options...
pwired Posted October 2, 2014 Author Share Posted October 2, 2014 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 More sharing options...
netcarver Posted October 2, 2014 Share Posted October 2, 2014 Just want to point out that the code diogo posted in #17 uses a relatively new PHP array syntax. If you are running PHP < 5.4 then you will need to do this instead... $images = array("image1.jpg", "image2.jpg", "image3.jpg"); 5 Link to comment Share on other sites More sharing options...
Recommended Posts