99Enzo99 Posted July 13, 2010 Share Posted July 13, 2010 Hello All, I have been using basic PHP for a while, but just in need of my first if/ifelse statement and things are going slightly tits up with an '<img src' variable. Basically the script is stuttering when I try to mix html and php... any ideas on why things run until I call the variable? Below is the script in the file and below that the output from 'view page source'... as you can see the thing falls down as soon as I try to call the image name from the database. <?php include('includes/conn_mysqli.inc.php'); // create database connection $conn = dbConnect('XXXXXXX'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Image By... :: Photography and T Shirts</title> <link href="flashbum.css" rel="stylesheet" type="text/css" /> </head> <body> <table align="center" width="930" border="0"> <tr> <td> <div class="header"></div> <?php $sql = 'SELECT * FROM images ORDER BY article_id ASC'; $result = $conn->query($sql); while ($row = $result->fetch_assoc()) { ?> <?php if ($orientation = 'landscape'){ echo '<div class=imageholderlandscape>'; echo '<div class=landscape>'; echo '<img src="gallery/$image" width="300" height="200" />'; echo '<div class=text>'; echo '<p><?php echo $row[dateadded]; ?></p>'; echo '</div>'; echo '</div>'; echo '<b><?php echo $row[item_name]; ?></b><br />'; echo '<?php echo $row[imagedescription]; ?>'; echo '</div>'; echo '<div class=spacer></div>'; } ?> <?php } ?> <div class="footer"></div> [code=php:0] -------------------------------------- From page source [code] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Image By... :: Photography and T Shirts</title> <link href="flashbum.css" rel="stylesheet" type="text/css" /> </head> <body> <table align="center" width="930" border="0"> <tr> <td> <div class="header"></div> <div class=imageholderlandscape><div class=landscape><img src="gallery/$image" width="300" height="200" /><div class=text><p><?php echo $row[dateadded]; ?></p></div></div><b><?php echo $row[item_name]; ?></b><br /><?php echo $row[imagedescription]; ?></div><div class=spacer></div> <div class=imageholderlandscape><div class=landscape><img src="gallery/$image" width="300" height="200" /><div class=text><p><?php echo $row[dateadded]; ?></p></div></div><b><?php echo $row[item_name]; ?></b><br /><?php echo $row[imagedescription]; ?></div><div class=spacer></div> <div class=imageholderlandscape><div class=landscape><img src="gallery/$image" width="300" height="200" /><div class=text><p><?php echo $row[dateadded]; ?></p></div></div><b><?php echo $row[item_name]; ?></b><br /><?php echo $row[imagedescription]; ?></div><div class=spacer></div> <div class="footer"></div> [code] ------------------------------------------------------------ All help gratefully recieved Quote Link to comment Share on other sites More sharing options...
trq Posted July 13, 2010 Share Posted July 13, 2010 Variables are not interpolated within single quotes. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 13, 2010 Share Posted July 13, 2010 This "if" statement is wrong: <?php if ($orientation = 'landscape'){ ?> One "=" for assignment, two "==" for comparison <?php if ($orientation == 'landscape'){ ?> Ken Quote Link to comment Share on other sites More sharing options...
99Enzo99 Posted July 13, 2010 Author Share Posted July 13, 2010 I tried changing the if statement... double (==) returns nothing within the if statement single (=) returns upto calling the $image variable View source shows <img src="gallery/$image" instead of <img src="gallery/imagename.jpg etc Any more ideas? Quote Link to comment Share on other sites More sharing options...
TOA Posted July 13, 2010 Share Posted July 13, 2010 Variables are not interpolated within single quotes. Thorpe's right, look into interpolation. For example: instead of: echo '<p><?php echo $row[dateadded]; ?></p>'; do it this way: echo '<p>'.$row[dateadded].'</p>'; And to address your direct topic: echo '<img src="gallery/'.$image.'" width="300" height="200" />'; Hope that helps Quote Link to comment Share on other sites More sharing options...
99Enzo99 Posted July 13, 2010 Author Share Posted July 13, 2010 Nice one, DevilsAdvocate It worked a treat and does everything I need... I'm not going to mark it as solved just in case I need to come back when the ifelse statement fails... However if it works no problem I will pop back and mark it as such Quote Link to comment Share on other sites More sharing options...
TOA Posted July 13, 2010 Share Posted July 13, 2010 Glad to help Quote Link to comment Share on other sites More sharing options...
99Enzo99 Posted July 13, 2010 Author Share Posted July 13, 2010 Hey, hey... What am I doing wrong? Took your advice and then put in the else statement... the else should report a portrait image as the if reports the landscape... anyway... tried a few combinations and looked at a few websites but I think I'm doing everything right... <div class=imageholderlandscape><div class=landscape><img src="gallery/newlandsvalley.jpg" width="300" height="200" /><div class=text><p>10th July 10</p></div></div><b>Newlands Valley</b><br />Newlands Valley is a stunning place to see. Just South West of Keswick, it is a awe inspiring walk or drive.<br /> <br /></div><div class=spacer></div> The images are showing as they should with the format correct... but, there are three images, two are set to 'landscape' in the database and one is set to 'portrait' yet they are all showing as landscape. The php is as follows <?php $sql = 'SELECT * FROM images ORDER BY article_id ASC'; $result = $conn->query($sql); while ($row = $result->fetch_assoc()) { ?> <?php if ($orientation = 'landscape'){ echo '<div class=imageholderlandscape>'; echo '<div class=landscape>'; echo '<img src="gallery/'.$row[image].'" width="300" height="200" />'; echo '<div class=text>'; echo '<p>'.$row[dateadded].'</p>'; echo '</div>'; echo '</div>'; echo '<b>'.$row[item_name].'</b>'; echo '<br />'.$row[imagedescription].'</div>'; echo '<div class=spacer></div>'; }else{ echo '<div class=imageholderportrait>'; echo '<div class=portrait>'; echo '<img src="gallery/'.$row[image].'" width="200" height="300" />'; echo '<div class=text>'; echo '<p>'.$row[dateadded].'</p>'; echo '</div>'; echo '</div>'; echo '<b>'.$row[item_name].'</b>'; echo '<br />'.$row[imagedescription].'</div>'; echo '<div class=spacer></div>'; } ?> [code=php:0] Which section is it that is trying to make me look stupid? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 13, 2010 Share Posted July 13, 2010 Like I said, the "if" statement is wrong. <?php if ($orientation = 'landscape'){ ?> One "=" is for assignment, two "==" is for comparison. What's happening here is that you're assigning the string 'landscape' to the variable $orientation which always returns "true" and the if statement succeeds. You need to use <?php if ($orientation == 'landscape'){ ?> What I don't see is where the variable $orientation is being set in the first place. Ken Quote Link to comment Share on other sites More sharing options...
99Enzo99 Posted July 13, 2010 Author Share Posted July 13, 2010 Thanks for the repeat Ken, I understand that instruction now The portrait works (as they are all returning portrait (but I understand why))... however the variable $orientation is in the database. I guess all portrait because I havent set it anywhere on the page, just thought it would be read from the sql Where and how does it need to be set? ...first time into setting variables as I have always just drawn them straight... Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 13, 2010 Share Posted July 13, 2010 If the 'orientation' is in the database you have to get the value from the $row, probably like this: <?php if ($row['orientation'] == 'landscape'){ ?> It would also be good if you indent your code properly as it makes debugging easier: <?php $sql = 'SELECT * FROM images ORDER BY article_id ASC'; $result = $conn->query($sql); while ($row = $result->fetch_assoc()) { if ($row['orientation'] == 'landscape'){ echo '<div class=imageholderlandscape>'; echo '<div class=landscape>'; echo '<img src="gallery/'.$row[image].'" width="300" height="200" />'; echo '<div class=text>'; echo '<p>'.$row['dateadded'].'</p>'; echo '</div>'; echo '</div>'; echo '<b>'.$row['item_name'].'</b>'; echo '<br />'.$row['imagedescription'].'</div>'; echo '<div class=spacer></div>'; }else{ echo '<div class=imageholderportrait>'; echo '<div class=portrait>'; echo '<img src="gallery/'.$row['image'].'" width="200" height="300" />'; echo '<div class=text>'; echo '<p>'.$row['dateadded'].'</p>'; echo '</div>'; echo '</div>'; echo '<b>'.$row['item_name'].'</b>'; echo '<br />'.$row['imagedescription'].'</div>'; echo '<div class=spacer></div>'; } ?> Looking at your code, I shortened it a little by making a function to create the string to echo. <?php function disp_pic($row,$w,$h) { $tmp = array(); $tmp[] = '<div class="imageholder' . $row['orientation'] . '">'; $tmp[] = '<div class="' . $row['orientation'] . '">'; $tmp[] = '<img src="gallery/' . $row['image'] . '" width="' . $w . '" height="' . $h . '" />'; $tmp[] = '<div class="text">'; $tmp[] = '<p>' . $row['dateadded'] . '</p>'; $tmp[] = '</div>'; $tmp[] = '</div>'; $tmp[] = '<b>' . $row['item_name'] . '</b>'; $tmp[] = '<br />' . $row['description'] . '</div>'; $tmp[] = '<div class="spacer"></div>'; return(implode("\n",$tmp) . "\n"; } $sql = 'SELECT * FROM images ORDER BY article_id ASC'; $result = $conn->query($sql); while ($row = $result->fetch_assoc()) { echo disp_pic($row,$w,$h); } ?> Ken Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 13, 2010 Share Posted July 13, 2010 Oops.. change <?php return(implode("\n",$tmp) . "\n"; ?> to <?php return(implode("\n",$tmp) . "\n"); ?> in my function. Ken Quote Link to comment Share on other sites More sharing options...
99Enzo99 Posted July 13, 2010 Author Share Posted July 13, 2010 Thanks Ken, that is brilliant... The first code works a treat... there was one small problem as there was a termination '}' that was missing. As for your shortened version I will take a little time to digest the code, but digest I will. Stunning... after a couple of nights of pulling hair out you really helped me on the road to a regaining a full head of hair. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.