Jump to content

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 />' ;
}

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.