php output to table ???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pha3dr0n
Forum Newbie
Posts: 9
Joined: Mon Sep 02, 2002 9:12 am

php output to table ???

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
pha3dr0n
Forum Newbie
Posts: 9
Joined: Mon Sep 02, 2002 9:12 am

Post 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.
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post 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,
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
pha3dr0n
Forum Newbie
Posts: 9
Joined: Mon Sep 02, 2002 9:12 am

Post 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
Post Reply