If, else, foreach : help

Hi all
What I have below is a small snippet of what returns a list of clubs from the DB, everything works great. But, some clubs don’t have a url value pointing to another page for more information.

How can I code another else? if? so I still show all the clubs but the ones that don’t have a ‘url’ remove the <a href=“venue/…”> from the code.

<? if($num_rows): ?><dl class="clubs">
<? foreach($DATA as $row): ?>
        
<dt><a href="venue/<?=$row['url']?>"><?=$row['title']?></a></dt>
                <dd><em><?=$row['short']?></em>
            </dd>

<? endforeach ?></dl>
<? else: ?>
 <p>No data found</p>
<? endif ?>

example:

<dt><a href="venue/<?=$row['url']?>"><?=$row['title']?></a></dt>
                <dd><em><?=$row['short']?></em>
            </dd>
<dt><?=$row['title']?></dt>
                <dd><em><?=$row['short']?></em>
            </dd>

Hope this makes sense thanks in advance :slight_smile:

I’m personally not a fan of the verbose foreach: endforeach syntax. You don’t get bracket matching in your text editor for starters, and it’s more typing and reading.

Try this:


<?php
if($num_rows) {
  
	$clubs = '<dl class="clubs">';

	foreach($DATA as $row) {
		$title = htmlentities($row['title']);

		//Wrap in link 
		if($row['url']) {
			$url = htmlentities($row['url']);
			$title = "<a href='venue/$url'>$title</a>";
		}
		
		$clubs .= "<dt>$title</dt>" .
				  "<dd><em>" . htmlentities($row['short']) . "</em></dd>\
";
	
	}
	
	echo $clubs . "</dl>";
}
else {
	echo "<p>No clubs found</p>";
}
?>

(update: posted same time cranial-bore, thanks :slight_smile:
I also have a img that fits into this, its late in UK I’m going to look into this more tomorrow if you about thanks again cranial-bore )

This is what I mean, seems to work ok besides some html styling not matching.
I just wasn’t sure if I could add another if else inside a current foreach loop.
Still very much a php novice thanks for any advice.

<? if($num_rows): ?><dl class="clubs">
<? foreach($DATA as $row): ?>
        
            <dt>
                 <? if($row['url'] == ''):?>

                <?=$row['title']?>

                <? else: ?>

                <a href="venue/<?=$row['url']?><?=$row['title']?></a>

                <?php endif; ?>

            </dt>
                <dd><em><?=$row['short']?></em>
            </dd>

<? endforeach ?></dl>
<? else: ?>
 <p>No data found</p>
<? endif ?>

thanks :cool: