freelancer Posted October 3, 2009 Share Posted October 3, 2009 Hello. I ran into problem with my code. I select data from sql database from row that can contain data like this: image1.jpg, image2.jpg, image3.jpg, image4.jpg OR is empty. I display these images + thumbnails on my site with following code: <?php $ss = explode(',',$key['ss']); $sscount = count($ss); $loc = 'upload/'; $pre = 't_'; for ( $i = 0; $i < $sscount; $i++ ) $screen[] = "<a href='$loc$ss[$i]' onclick='return hs.expand(this)'><img src='$loc$pre$ss[$i]' width='100px' height='60px'></a> " ?> And on showing results on specific location like this: <?php echo $screen[0], $screen[1], $screen[2], $screen[3] ?> Everything is perfect when there is atleast 1 screenshot path in database, but when there is no screenshot and sql row is empty then I want it to display nothing, because this code still prints <img> with no src and shows empty picture when I have 0 data in database. Hope you understood my issue, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/176391-solved-making-loop-to-echo-nothing-if-value-doesnt-exist/ Share on other sites More sharing options...
cags Posted October 3, 2009 Share Posted October 3, 2009 I sort of understand your problem, but think it would help if we could see a bit more code. You mention that <img > still appears, but you don't show the line that is on, it would be helpfull to see that line and possible a line or two either side of it. Quote Link to comment https://forums.phpfreaks.com/topic/176391-solved-making-loop-to-echo-nothing-if-value-doesnt-exist/#findComment-929704 Share on other sites More sharing options...
freelancer Posted October 3, 2009 Author Share Posted October 3, 2009 Well. That's basically it. All database stuff is in different table locations, this is where screenshot thumbnails are shown and with highslide script will be expanded on click. <tr> <td colspan="2"><div id="media" align="center" class="highslide"> <?php echo $screen[0], $screen[1], $screen[2], $screen[3] ?></div> </td> <tr> With images loaded page source code is: <tr> <td colspan="2"><div id="media" align="center" class="highslide"> <a href='upload/image1.jpg' onclick='return hs.expand(this)'><img src='upload/t_image1.jpg' width='100px' height='60px'></a> </div> </td> </tr> Without image: <tr> <td colspan="2"><div id="media" align="center" class="highslide"> <a href='upload/' onclick='return hs.expand(this)'><img src='upload/t_' width='100px' height='60px'></a> </div> </td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/176391-solved-making-loop-to-echo-nothing-if-value-doesnt-exist/#findComment-929706 Share on other sites More sharing options...
Alex Posted October 3, 2009 Share Posted October 3, 2009 Yea, it would be good to see more code, preferably the part where you're preforming the query. Because I don't think that for statement is needed. You should probably be limiting the rows selected in your query to eliminate the records with empty image columns anyway. Quote Link to comment https://forums.phpfreaks.com/topic/176391-solved-making-loop-to-echo-nothing-if-value-doesnt-exist/#findComment-929707 Share on other sites More sharing options...
freelancer Posted October 3, 2009 Author Share Posted October 3, 2009 Ok. Query part starts with function <?php $id=$_GET['id']; if(isset($id)) { $info = getResult($id); foreach($info as $key) { // Screenshots explode $ss = explode(',',$key['ss']); $sscount = count($ss); $loc = 'upload/'; $pre = 't_'; for ( $i = 0; $i < $sscount; $i++ ) $screen[] = "<a href='$loc$ss[$i]' onclick='return hs.expand(this)'><img src='$loc$pre$ss[$i]' width='100px' height='60px'></a> " ?> getResult functions is: <?php function getResult($id) { $conn = connect(); $sql = "SELECT * FROM results WHERE id='$id'"; $result = mysql_query($sql); if($result) { $row = mysql_fetch_assoc($result); $resultinfo[] = $row; } return $resultinfo; closeconnection($conn); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/176391-solved-making-loop-to-echo-nothing-if-value-doesnt-exist/#findComment-929711 Share on other sites More sharing options...
Alex Posted October 3, 2009 Share Posted October 3, 2009 You can probably fix it just by changing the query to this: $sql = "SELECT * FROM results WHERE id='$id' AND ss!=''"; Wait, if you're only fetching one row why are you doing a foreach? Why don't you just do something like: $result = mysql_query(SELECT * FROM results WHERE id='$id' AND ss!=''); $row = mysql_fetch_assoc($result); $ss = explode(',', $row['ss']); ... Quote Link to comment https://forums.phpfreaks.com/topic/176391-solved-making-loop-to-echo-nothing-if-value-doesnt-exist/#findComment-929714 Share on other sites More sharing options...
freelancer Posted October 3, 2009 Author Share Posted October 3, 2009 I use foreach because there are actually more results that I select. Whole table looks actually like this: <table id="details" width="450" border="0" align="center" cellpadding="2" cellspacing="0"> <tr> <td height="50" width="145"></td> <td width="72"><div class="score" align="center" style="color:<?php echo $color1 ?>"><h3><?php echo $score[0]; ?></h3></div></td> <td width="72"><div class="score" align="center" style="color:<?php echo $color2 ?>"><h3><?php echo $score[1]; ?></h3></div></td> <td height="50" width="145"><div align="center"> <h2><?php echo $key['opponent']; ?></h2> </div></td> </tr> <tr> <td height="42" colspan="4"><div><h1 align="center">Match details</h1></div></td> </tr> <tr> <td height="21" colspan="2"><p align="right"><strong>Date</strong></p></td> <td height="21" colspan="2"><div class="entry"><?php echo $key['date']; ?></div></td> </tr> <tr> <td height="21" colspan="2"><p align="right"><strong>League</strong></p></td> <td height="21" colspan="2"><div class="entry"><?php echo $key['league']; ?></div></td> </tr> <tr> <td height="21" colspan="2"><p align="right"><strong>Type</strong></p></td> <td height="21" colspan="2"><div class="entry"><?php echo $key['type']; ?></div></td> </tr> <tr> <td height="40" colspan="4"><div><h1 align="center">Opponent</h1></div></td> </tr> <tr> <td height="21" colspan="2"><p align="right"><strong>Tag</strong></p></td> <td height="21" colspan="2"><div class="entry"><?php echo $key['tag']; ?></div></td> </tr> <tr> <td height="21" colspan="2"><p align="right"><strong>Country</strong></p></td> <td height="21" colspan="2"><div class="entry"><?php echo $key['country']; ?></div></td> </tr> <tr> <td height="21" colspan="2"><p align="right"><strong>Lineup</strong></p></td> <td height="21" colspan="2"><div class="entry"><?php echo $key['lineup']; ?></div></td> </tr> <tr> <td height="21" colspan="2"><p align="right"><strong>Website</strong></p></td> <td height="21" colspan="2"><div class="entry"><a href="http://<?php echo $key['website']; ?>">Link</a></div></td> </tr> <tr> <td height="39" colspan="4"><div align="center"> <h1>Maps</h1> </div></td> </tr> <tr> <td height="39" colspan="2"><div> <h1 align="center">Screenshots</h1> </div></td> <td height="39" colspan="2"><div> <h1 align="center">Demos</h1> </div></td> </tr> <tr> <td colspan="2"><div id="media" align="center" class="highslide"> <?php echo $screen[0], $screen[1], $screen[2], $screen[3] ?></div> </td> <td colspan="2"></td> </tr> <tr> <td height="21" colspan="4"></td> </tr> <tr> <td colspan="4"></td> </tr> <tr> <td height="21" colspan="4"><div align="center"><a href="?p=results">Return to results</a></div></td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/176391-solved-making-loop-to-echo-nothing-if-value-doesnt-exist/#findComment-929717 Share on other sites More sharing options...
Alex Posted October 3, 2009 Share Posted October 3, 2009 Because I don't feel like rewriting your whole code, I guess a quick fix could be like: $screen[] = (!empty($ss[$i])) ? "<a href='$loc$ss[$i]' onclick='return hs.expand(this)'><img src='$loc$pre$ss[$i]' width='100px' height='60px'></a>" : NULL; Quote Link to comment https://forums.phpfreaks.com/topic/176391-solved-making-loop-to-echo-nothing-if-value-doesnt-exist/#findComment-929727 Share on other sites More sharing options...
freelancer Posted October 3, 2009 Author Share Posted October 3, 2009 Oh right! Thanks a lot mate, was thinking of it but couldn't really figure out how it would look like. That made it Quote Link to comment https://forums.phpfreaks.com/topic/176391-solved-making-loop-to-echo-nothing-if-value-doesnt-exist/#findComment-929739 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.