Jump to content

[SOLVED] Display Problems


patsypoopa

Recommended Posts

I've just posted a problem and it was solved however now i've got another problem with displaying the solution :P Oh and i'm still a newbie!

 

Right, i've got a loop which SHOULD display columns and records from a table. In a format like this for each record:

 

Name: Blah

Age: Bleh

 

It does this fine for the first record but for the 2nd record it only has the cell so its showing like this:

 

: Blah

: Bleh

 

Hope someone has the solution thanks either way

 

$sql = "SELECT * FROM $chosencollection";
$result = mysql_query($sql);
$result2 = mysql_query($sql);

$cols_num = mysql_num_fields($result2);

?>
<h1>View Collection</h1>
<?
echo "<p><h2>" . $collectionnametitle ."</h2></p>";

while($row = mysql_fetch_row($result))
{
    foreach($row as $cell)
        {
	 	$col = mysql_fetch_field($result2);
		$col->name = str_replace("_", " ", $col->name);
		echo "<b>{$col->name}</b>: $cell<br>";
	}
	echo "<br>";
}

Link to comment
https://forums.phpfreaks.com/topic/101887-solved-display-problems/
Share on other sites

well first off,

there is a few HTML errors in your coding,

 

replace:

echo "<p><h2>" . $collectionnametitle ."</h2></p>";

 

with:

echo "<h2>" . $collectionnametitle ."</h2>";

 

try this code:

 

$sql = "SELECT * FROM $chosencollection";
$result = mysql_query($sql);
$result2 = mysql_query($sql);

$cols_num = mysql_num_fields($result2);

?>
<h1>View Collection</h1>
<?
echo "<p><h2>" . $collectionnametitle ."</h2></p>";

while($row = mysql_fetch_row($result))
{
    foreach($row as $cell)
        {
	 	$col = mysql_fetch_field($result2);
		$col_name = str_replace("_", " ", $col->name);
		echo "<b>$col_name</b>: $cell<br>";
	}
	echo "<br>";
}

try

<?php  
$sql = "SELECT * FROM $chosencollection";
$result = mysql_query($sql);

echo '<h1>View Collection</h1>';

echo "<h2>$collectionnametitle </h2>";

while($row = mysql_fetch_assoc($result))
{
     foreach ($row as $fld => $value)
     {
        echo "$fld : $value <br />";
     }
     echo '<br />' ;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.