Page 1 of 1

php output to table ???

Posted: Mon Sep 02, 2002 9:12 am
by pha3dr0n
I am having difficulty trying to get a search result to display to a table. At the moment it lists the output all vertically ie.,

1. title1
1. genre1
1. format1
2. title2
2. genre2
2. format2

I really want it to display like:

title1 | genre1 | format1
title2 | genre2 | format2

Here's the script (edited for security reasons):

<html>
<head>
<title>Movies Search Results</title>
</head>

<body>
<h1>Movies Search Results</h1>
<?
trim($searchterm);
if (!$searchtype || !$searchterm)
{
echo "You have not entered search details. Please go back and try again.";
exit;
}

$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);

@ $db = mysql_pconnect("*****","*****","*****");

if (!$db)
{
echo "Error: Could not connect to database. Please try again later.";
exit;
}

mysql_select_db("*****");
$query = "select * from movies where ".$searchtype." like
'%".$searchterm."%'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);

echo "<p>Number of entries found: ".$num_results."</p>";

for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo "<p><strong>".($i+1).". Title: ";
echo htmlspecialchars( stripslashes($row["title"]));
echo "<p><strong>".($i+1).". Genre: ";
echo htmlspecialchars( stripslashes($row["genre"]));
echo "<p><strong>".($i+1).". Format: ";
echo htmlspecialchars( stripslashes($row["format"]));
echo "<p><strong>".($i+1).". No of CD's: ";
echo htmlspecialchars( stripslashes($row["cds"]));
echo "</p>";
}

?>

</body>
</html>

Any help would be appreciated !!! I am still trying to work it out myself, but if anybody can spot the problem please let me know.

Cheers,

Pha3dr0n

Posted: Mon Sep 02, 2002 9:26 am
by twigletmac
You need to change the HTML that is being used to format the page.

Try this while loop instead of the for loop and play with the HTML to format your results the way you would like:

Code: Select all

echo '&lt;p&gt;Title | Genre | Format | No of CD''s&lt;/p&gt;';
echo '&lt;p&gt;';
while ($row = mysql_fetch_array($result)) {
    echo htmlspecialchars(stripslashes($row&#1111;'title'])).' | ';
    echo htmlspecialchars(stripslashes($row&#1111;'genre'])).' | ';
    echo htmlspecialchars(stripslashes($row&#1111;'format'])).' | ';
    echo htmlspecialchars(stripslashes($row&#1111;'cds'])); 
    echo '&lt;br /&gt;';
} 
echo '&lt;/p&gt;';
Mac

Posted: Mon Sep 02, 2002 9:52 am
by pha3dr0n
Thanks for the reply - I have to admit though that I am somewhat out of my depth here. I applied the code that you suggested, and although it listed the data correctly, it gave a corresponding number of lines at the bottom :Title | Genre | Format | No of CD''s repeated over and over (corresponding to the number of results found.

Unfortunately I am one of those n00bies that use dreamweaver for html (although I edit it via notepad), and do not at the moment know how to format this so the data outputs to a table, or how to remove all the Title | Genre | Format | No of CD''s lines at the bottom.

Posted: Mon Sep 02, 2002 9:56 am
by gite_ashish
hi,

to give the actual HTML <TABLE> effect, you can try this code:

Code: Select all

&lt;?php

echo '&lt;TABLE&gt;';
echo '&lt;TR&gt;&lt;TH&gt;Title&lt;/TH&gt;&lt;TH&gt;Genre&lt;/TH&gt;&lt;TH&gt;Format&lt;/TH&gt;&lt;TH&gt;No of CDs&lt;/TH&gt;&lt;/TR&gt;';

while ( $row = mysql_fetch_array( $result ) )
{
    echo '&lt;TR&gt;';
    echo '&lt;TD&gt;' . htmlspecialchars( stripslashes( $row&#1111;'title'] ) ) . '&lt;/TD&gt;';
    echo '&lt;TD&gt;' . htmlspecialchars( stripslashes( $row&#1111;'genre'] ) ) . '&lt;/TD&gt;';
    echo '&lt;TD&gt;' . htmlspecialchars( stripslashes( $row&#1111;'format'] ) ) . '&lt;/TD&gt;';
    echo '&lt;TD&gt;' . htmlspecialchars( stripslashes( $row&#1111;'cds'] ) ) . '&lt;/TD&gt;';
    echo '&lt;/TR&gt;';
}

echo '&lt;/TABLE&gt;';

?&gt;

regards,

Posted: Mon Sep 02, 2002 9:57 am
by mikeq
Go into dreamweaver and create a table to the look you want, then have a look at the HTML it produces and apply this to your PHP code.

Posted: Mon Sep 02, 2002 10:01 am
by twigletmac
HTML is not really that difficult to get to grips with, you might want to consider doing a search on the web for a tutorial as basic formatting like tables and paragraphs etc. is something that is quick to learn and will make it a lot easier for you to format your data in the way you would like.

Mac

Posted: Mon Sep 02, 2002 11:05 am
by pha3dr0n
gite_ashish - that sorted it - thank you very much :P

twigletmac and mikeq - thank you both for your help and advise - I only started looking at html, php, mysql, and perl a week ago, so its a lot to take in - having said that I feel I have learnt a lot in a week, and am enjoying it at the same time :D

I have the funny feeling I may become a regular on this forum 8)

Cheers,

Pha3dr0n