Jump to content

[SOLVED] Making loop to echo nothing if value doesn't exist


freelancer

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
}
?>

Link to comment
Share on other sites

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']);
...

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.