How to display HTML in PHP

Hi all, been on and off with PHP just need a recap on how to code the below.

I basically have a number of venues coming from the DB and each venues properties need it’s own <dl><dt> etc…

Code:
//get the results

<?php
	
	$sql = "SELECT name, img, address, comments, website FROM venues 
	WHERE category='$category'";
	$result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    echo.... ???
?>

//output the result with HTML
I need a way of displaying (echoing) the html automatically with each record inside the PHP code, so it adds a new <dl> every time it finds another venue, Instead of dropping each variable inside the HTML.

<dl>
	
	<dt class="thumb-img"><?php echo $row['img'] ?></dt>
	<dt><?php echo $row['name'] ?></dt>
	<dt><?php echo $row['address'] ?></dt>
	
	<dd><p><?php echo $row['comments'] ?>, <?php echo $row['website'] ?></p></dd>

</dl>

Any suggestions? Thanks :cool:

Managed to sort the problem:


<?php
	
		$sql = "SELECT venue, img, address, comments, website FROM venues 
		WHERE category=1";
		$result = mysql_query($sql) or die(mysql_error());
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		echo'<dl>
		<dt class="thumb-img">' . $row['img'] .'</dt>
		<dt>' . $row['venue'] . '</dt>
		<dt>' . $row['address'] . '</dt>
		<dd><p>' . $row['comments'] .','. $row['website'] .'</p></dd></dl>';
		}


 		?>


Any room for improvement? :cool:

Some.
Make it in 2 steps an use more HTML friendly syntax.
First step: getting data

<?php
        $sql = "SELECT venue, img, address, comments, website FROM venues
        WHERE category=1";
        $result = mysql_query($sql) or die(mysql_error());
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) $DATA[]=$row;
?>

Second step: displaying data.
Put this code into separate php file, along with other necessary HTML:


<table>
<? foreach($DATA as $row): ?>
        <dl>
        <dt class="thumb-img"><?=$row['img']?></dt>
        <dt><?=$row['venue']?></dt>
        <dt><?=$row['address']?></dt>
        <dd><p><?=$row['comments']?>, <?=$row['website']?></p></dd></dl>
<? endforeach ?>
</table>
 ?>

It could not work, due to some stupid setting. You can both change short_open_tags php.ini setting or use <?php echo $var ?> instead of neat <?=$var?> syntax.

The disabling of short tags is not a stupid setting if you use xml. How would the server know if you intend to use XML (<?xml) or PHP (<?php) if you just have <? also AFAIK short tags will be depreciated with php6

No, they won’t.

How would the server know if you intend to use XML (<?xml)

By extension. File extension.
Server distinguish file types by it’s extension. For PHP file it’s .php and for XML it’s .xml
Very simple.

For these rare occurrences when we need to print out XML from the PHP file, simple

echo '<?xml-or-whatever>'; 

would help.

Yes of course.
Unlike html(separation of html & php):

<table>
<tr>
<td><?php echo $td_data; ?></td>
</tr>
</table>

we can’t peform for xml:

<?xml version="1.0" encoding="UTF-8"?>
<?php foreach($datas as $data): ?>
<parentTag>
<childTag><?php echo $data['field']; ?></childTag>
</parentTag>
<?php endforeach; ?>

Run this file and notice the error.
Just an example.

Yes. And you can notice the simple solution I gave below.
If it was not clear enough:

echo '<?xml version="1.0" encoding="UTF-8"?>';
<?php foreach($datas as $data): ?>
<parentTag>
<childTag><?php echo $data['field']; ?></childTag>
</parentTag>
<?php endforeach; ?>

And of course it is up to you and your situation.
In mine, I have thousands <?='s versus one echo ‘<?xml’
So, I decide to have short open tags enabled for huge usability advantage.
If you have inverse ratio, you can turn it off.

Going over my head a bit now… are you saying it will be better to feed the page from a XML file instead? My main problem why I wanted the HTML in the PHP is because I need to repeat through the records, I cant just have static HTML.

I tried:

<? foreach($DATA as $row): ?> //line 61
        <dl>
        <dt class="thumb-img"><?php echo $row['photo']?></dt>
        <dt><?php echo $row['venue_id']?></dt>
        <dt><?php echo $row['address']?></dt>
        <dd><p><?php echo $row['comments']?>, <?php echo $row['website']?></p></dd></dl>
<? endforeach ?>

Error: Warning: Invalid argument supplied for foreach() in… line 61

Why is this way better than what I already have? :cool:

I’d also like to add something like redir.php?url= to $row[‘website’] to update the ‘hits’ column also in the DB, how can I add some code to track the click through? :slight_smile:

update: I now understand the foreach regarding repeating the code, thanks

are you saying it will be better to feed the page from a XML file instead?

No. XML has no relation to your case at all. It was just a side note.

Why is this way better than what I already have?

It looks better to me because HTML tags remains HTML tags, without all these '," and ;

I’d also like to add something like redir.php?url= to $row[‘website’]

so, add it. As if there was plain HTML. <a href="redir.php?url=… and so on.

It looks better to me because HTML tags remains HTML tags, without all these '," and ;
:slight_smile:

so, add it. As if there was plain HTML. <a href="redir.php?url=… and so on.
What sort of code will I need to add & where? redir.php?url was just an example I seen somewhere. Can I just add some sort of snippet at the beginning of the page and call it’s self?

I cant view any records using the foreach method, as above

Error: Warning: Invalid argument supplied for foreach() in… line 61
???

Thanks Shrapnel_N5 :cool:

What sort of code will I need to add & where?

well, it is separate question and better to finish first one first.

Warning: Invalid argument supplied for foreach() in… line 61

Looks like you don’t have $DATA array.
how do you use this code? do you include it into main script? I hope you include it as a file, not as an HTTP URL

Ok one a time thanks.

I have both the snippets of code place directly on the page (is that what you mean):

<h1>Title</h1>
<div>somecontent</div>

<?php
        $sql = "SELECT venue_id, photo, comments, website, address FROM venues
        WHERE category=1";
        $result = mysql_query($sql) or die(mysql_error());
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) $DATA[]=$row;
?>
<? foreach($DATA as $row): ?> //line 61
        <dl>
        <dt class="thumb-img"><?php echo $row['photo']?></dt>
        <dt><?php echo $row['venue_id']?></dt>
        <dt><?php echo $row['address']?></dt>
        <dd><p><?php echo $row['comments']?>, <?php echo $row['website']?></p></dd></dl>
<? endforeach ?>

<footer></footer> …

I’m not much of an expert on PHP thanks :slight_smile:

Does your query return any data?
Anyway, It was my fault - $DATA variable must be initialized.
So, this is better:

<?php
        $sql = "SELECT venue_id, photo, comments, website, address FROM venues
        WHERE category=1";
        $result = mysql_query($sql) or die(mysql_error());
        $DATA=array();
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) $DATA[]=$row;
        $num_rows=mysql_num_rows($result);
?>
<? if($num_rows): ?>
  <? foreach($DATA as $row): ?>
        <dl>
        <dt class="thumb-img"><?= $row['photo']?></dt>
        <dt><?= $row['venue_id']?></dt>
        <dt><?= $row['address']?></dt>
        <dd><p><?= $row['comments']?>, <?= $row['website']?></p></dd></dl>
  <? endforeach ?>
<? else: ?>
 No data found
<? endif ?>

I’ve updated the code:

No data found

??

:blush: my fault… the category was wrong.

the first example now works, so does the second example. So which is one is best to use then? The first has less code.

cheers Shrapnel_N5 :slight_smile:

Up to you. Second one will say “No data found” when no data returned and first one just remained silent. I prefer second one.

what I mean is the first example:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) $DATA[]=$row;

second:

$DATA=array(); // this is not needed to work why add it?
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) $DATA[]=$row;
        $num_rows=mysql_num_rows($result); // or this

anyway everything’s working great now, onto the next problem:

I’ve updated my code, which works ok, each venue will link to another page now aswell a their website:

<?php foreach($DATA as $row): ?>
        <dl>
        <dt class="thumb-img"><img src="images/<?php echo $row['photo']?>"></dt>
        <dt><a href="venues/<?php echo str_replace(" ","-",(strtolower(ucwords($row['venue_id'])))) ?>"><?php echo $row['venue_id']?></a></dt>
        <dt><?php echo $row['address']?></dt>
        <dd><p><?php echo $row['comments']?>, <a href="redir.php?url=<?php echo $row['website']?>">website</a></p></dd></dl>
<?php endforeach ?>

How can I update the website hits with redir.php?url= (do I need this?) what code should i write?

:cool:

$DATA=array(); // this is not needed to work why add it?

We badly need it. Unconditionally. Each variable must be initialized before use.
Or we’ll have unpredicted results.
Imagine we have two similar code snippets. In the first one we fill $DATA variable with some data, and then… adding another! As a result $DATA will contain two data sets. And there is a lot of other reasons.

$num_rows=mysql_num_rows($result); // or this

yes, this can be skipped. But in many cases it will be very useful.

echo str_replace(" “,”-",(strtolower(ucwords($row[‘venue_id’]))))

In order to keep our template as plain and clean as possible, I’d suggest to do all preparations in the code part. But it is subject to discuss though.
I’d make it something like

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  $row['venue_id']=str_replace(" ","-",(strtolower(ucwords($row['venue_id']))))
  // any other preparations
  $DATA[]=$row;
}

How can I update the website hits with redir.php?url

By writing redir.php of course :slight_smile:
With just a few lines of code.
Write your hit to database and then use header() function to send Location HTTP header to the browser.