Jump to content

Recommended Posts

Hi all,

I'm pretty new to php and MySql and have hit a problem.

 

I am able to display data in a table using the following code:

 

<?php 

$database="MYDATABASE"; 

mysql_connect ("localhost", "MYUSERNAME", "MYPASSWORD"); 

@mysql_select_db($database) or die( "Unable to select database"); 

$result = mysql_query( "SELECT field1, field2, field3 FROM staff WHERE `group` = 'group1' ORDER BY `role` " ) 

or die("SELECT Error: ".mysql_error()); 

$num_rows = mysql_num_rows($result); 

print "There are $num_rows records.<P>"; 

print "<table width=200 border=1>\n"; 

while ($get_info = mysql_fetch_row($result)){ 

print "<tr>\n"; 

foreach ($get_info as $field) 

print "\t<td><font face=arial size=1/>$field</font></td>\n"; 

print "</tr>\n"; 

print "</table>\n"; 

?>

 

However, I would like to display the results using the following format:

 

<div id="info"><!--this is a div contanier for info"-->

    <h1> field1 </h1>

          <img src="field2" class="floatLeft" /><p>field3</p>

</div><!--close info div-->

 

Obviously, if there was more than one row of results, it would repeat the above format for each row.

 

Any help would be greatly appreciated

Thanks

Pete

<?php

$database = "MYDATABASE";
mysql_connect("localhost", "MYUSERNAME", "MYPASSWORD");
@mysql_select_db($database) or die("Unable to select database");
$result = mysql_query("SELECT field1, field2, field3 FROM staff WHERE `group` = 'group1' ORDER BY `role` ") or die("SELECT Error: ".
mysql_error());
$num_rows = mysql_num_rows($result);
print "There are $num_rows records.<P>";
print "<table width=200 border=1>\n";
while ($get_info = mysql_fetch_row($result)) {
print '<div id="info"><!--this is a div contanier for info"-->
    <h1>'.$get_info[0].'</h1>
           <img src="'.$get_info[0].'" class="floatLeft" /><p>'.$get_info[2].'</p>
</div><!--close info div-->';
}

?>

That should do it. Your div has the id of "info". If you want to have the div wrap each individually you should change from id to class otherwise your markup will be invalid

You can also overcome this by putting the div outside of the while loop, creating one container for all of the rows of data

Learn to use the

[code tags, they are your friend. Sounds like you one something like this...

while ($get_info = mysql_fetch_array($result)){ 
   echo '<div id="info"><!--this is a div contanier for info"-->';
   echo '<h1>' . $get_info[0] . '<h1>';
   echo '<img src="' . $get_info[1] . '" class="floatLeft" /><p>' . $get_info[2] . '</p>';
   echo '<div>';
}

Just a quick note, semantically speaking a page should only ever have one <h1> tag.

 

Edit: d'oh.

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.