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
php output to table ???
Moderator: General Moderators
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
Mac
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 '<p>Title | Genre | Format | No of CD''s</p>';
echo '<p>';
while ($row = mysql_fetch_array($result)) {
echo htmlspecialchars(stripslashes($rowї'title'])).' | ';
echo htmlspecialchars(stripslashes($rowї'genre'])).' | ';
echo htmlspecialchars(stripslashes($rowї'format'])).' | ';
echo htmlspecialchars(stripslashes($rowї'cds']));
echo '<br />';
}
echo '</p>';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.
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.
- gite_ashish
- Forum Contributor
- Posts: 118
- Joined: Sat Aug 31, 2002 11:38 am
- Location: India
hi,
to give the actual HTML <TABLE> effect, you can try this code:
regards,
to give the actual HTML <TABLE> effect, you can try this code:
Code: Select all
<?php
echo '<TABLE>';
echo '<TR><TH>Title</TH><TH>Genre</TH><TH>Format</TH><TH>No of CDs</TH></TR>';
while ( $row = mysql_fetch_array( $result ) )
{
echo '<TR>';
echo '<TD>' . htmlspecialchars( stripslashes( $rowї'title'] ) ) . '</TD>';
echo '<TD>' . htmlspecialchars( stripslashes( $rowї'genre'] ) ) . '</TD>';
echo '<TD>' . htmlspecialchars( stripslashes( $rowї'format'] ) ) . '</TD>';
echo '<TD>' . htmlspecialchars( stripslashes( $rowї'cds'] ) ) . '</TD>';
echo '</TR>';
}
echo '</TABLE>';
?>regards,
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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
Mac
gite_ashish - that sorted it - thank you very much
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
I have the funny feeling I may become a regular on this forum
Cheers,
Pha3dr0n
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
I have the funny feeling I may become a regular on this forum
Cheers,
Pha3dr0n