Forum Moderators: coopster

Message Too Old, No Replies

Call single result from list of results

Call up new page with more details about a single result

         

stuartindigo

1:01 pm on Sep 19, 2010 (gmt 0)

10+ Year Member



I want to make a list of results "clickable", such that more details about a single result can be shown on either a new page or an additional function on the same page (new page is better as it can be called from many different searches).

How do I pass all the variables from one row of an array retrieved and then display them differently.

So I would want to click a single row and display more details about the book - a picture, synopsis etc.

The code below builds the "short" table array - where do I go from here?


<?php
if (isset($_POST['submit'])) {
$boekcat = $_POST['boekcat'];
$con = mysql_connect("localhost","wordpress","encryptedpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("wordpress");

$query = ("SELECT * FROM wp_boekfly WHERE boekcat='$boekcat' ORDER BY AuthorSurname ");
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo $row['title'];
echo " " . $row['AuthorSurname'];
echo " " . $row['AuthorForename'];
echo "<br />";
}
}
?>

JAB Creations

3:11 pm on Sep 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All you have to do is use a conditional if statement inside of the while loop...

<?php
$query1 = "SELECT * FROM example";
$result = mysql_query($query1);

if ($result1)
{
while($row1 = mysql_fetch_array($result1))
{
if ($row1['id']=='123') {echo '<div><a href="members/'.$row1['user_url'].'">'.$row1['user_name'].'</a></div>';}
}
}
else {/*error handling for $query1*/}

mysql_data_seek($result1,0);
?>


Also you can reset the $row1 variable to start from item 0 instead of 1 (it will "creep" forward) by using the last line of code above.

Also I highly recommend always numbering your $query, $result, and $row variables to make it easy to log MySQL errors.

Additionally you should not use die or exit except to prevent flood requests or when you serve HTTP 304.

- John

stuartindigo

4:32 pm on Sep 19, 2010 (gmt 0)

10+ Year Member



That appears to be more for static pages, not for pages generated from further data in the database.

So an example output line might be

Spycatcher Wright Peter

Clicking on that line would then call up a page filled with other data from the database. Much like clicking an item in a shopping catalog to get more details.

Or would I be better calling a "fullpage.php?boekid" for each line (boekid being unique identifier) and then running a query to call and display the details for boekid in "fullpage.php", and how does that work?

stuartindigo

3:32 pm on Sep 21, 2010 (gmt 0)

10+ Year Member



I solved my problem with a couple of bits of code and it looks nice:

Here is the list page I made to query
<?php

$con = mysql_connect("localhost","wordpress","yadi_password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("wordpress");

$query = ("SELECT * FROM wp_boekfly ORDER BY boekid DESC LIMIT 12 ");
$result = mysql_query($query);

echo "<table border=0 width=70%>";
while($row = mysql_fetch_array($result))
{
echo "<tr><td><a href='fullboek.php?boekid=" . $row['boekid'] ."'>" . $row['title'] ."</a></td>";
echo "<td>" . $row['AuthorSurname'] ."</td>";
echo " <td>" . $row['AuthorForename'] ."</td>";
echo "</tr>";
}

?>




Clicking on a title then opens the new page with the boekid passed and everything can be prettied up and displayed as required:
<?php

$boekid = $_GET['boekid'];

$con = mysql_connect("localhost","wordpress","yadi_password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("wordpress");

$query = ("SELECT * FROM wp_boekfly WHERE boekid='$boekid' LIMIT 1");

$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo $row['title'];
echo " author" . $row['AuthorSurname'];
echo " " . $row['AuthorForename'];
echo "<br />";
}

?>


This also means that I can make it more efficient by only calling the items I need in the first page, and tidying up and releasing memory after I've got the table out.