Click to See Complete Forum and Search --> : Search Image Database by username?


PJStew
08-10-2006, 11:30 AM
I'm trying to make a simple search for my images, so you can seach by username I would also like if posible, to be able to search be style eg. (Trials, Freestyle, ect...) the code have done so far, will display 1 image by the user, but then I can't get it to go to the next. Plus I can only search by username.
<body>
<form action="<?php $PHP_SELF ?>" name="search" method="post">
<input type="text" name="username" />
<input type="submit" value="Search" />
</form>
<?php

$username = $_POST['username'];

$conn = mysql_connect("localhost","user","password")
or die("Could not connect");
$rs = mysql_select_db("images",$conn)
or die("Could not select database");

$sql2 = "SELECT COUNT(*) as `count` FROM uploaded_images WHERE user_name = '$username'";
$result = mysql_query($sql2) or die();
$row = mysql_fetch_array($result);
$numrows = $row['count'];

$p = (isset($_GET["pic"]) and is_numeric($_GET["pic"]) and ($_GET["pic"] > 0) and ($_GET["pic"] <= $numrows))
? $_GET["pic"]
: $numrows ;

$sql1 = "SELECT * FROM uploaded_images WHERE user_name = '$username' ORDER BY id LIMIT 1 OFFSET ".($p - 1);
$result = mysql_query($sql1) or die();
$r = mysql_fetch_assoc($result);




if($p)
{
echo "<a href=\"?p=exhibition&filename=" . ($p - 1) . "\" id=\"A3\">PREVIOUS</a> ";

echo " <a href=\"?p=exhibition&filename=" . ($p + 1) . "\" id=\"A2\">NEXT</a>";
}

?>
<?php echo '<div id="EXHIBITION"><img src="'.$r['filename'].'" alt=" "></div>'; ?>
</body>
</html>
Any ideas?

Doc Thirst
08-10-2006, 12:31 PM
I believe what you are looking for is a while statment

while (there are db results)
{
display results
}

PJStew
08-10-2006, 12:40 PM
I believe what you are looking for is a while statment

while (there are db results)
{
display results
}
Could you please edit the script so I can see what you meen?
Would it be something like this?
while ($r)
{
<?php echo '<div id="EXHIBITION"><img src="'.$r['filename'].'" alt=" "></div>'; ?>
}
Thanks :)

NogDog
08-10-2006, 12:42 PM
The most common idiom is to do the query, then:

while($row = mysql_fetch_assoc($queryResult))
{
// output results from array $row
}

PJStew
08-10-2006, 01:00 PM
I still can't get it to work. any more ideas? :confused:

bokeh
08-10-2006, 01:17 PM
Stew, I would run the query one time and store the result in a session variable. You can also store the current position within the numeric array in a session variable too to help with navigation. This will allow you to query the DB once per username or once per category for each client and end up with data that runs in numeric sequence that can be used for all related subsequent page loads.

And you don't need the while loop as you are only trying to display one picture per page load.

PJStew
08-10-2006, 01:57 PM
Stew, I would run the query one time and store the result in a session variable. You can also store the current position within the numeric array in a session variable too to help with navigation. This will allow you to query the DB once per username or once per category for each client and end up with data that runs in numeric sequence that can be used for all related subsequent page loads.

And you don't need the while loop as you are only trying to display one picture per page load.
Could you posible give me an exaple of how it would work with a next and prev button? at the mo. I can see it gets the info, as it knows how many images have been uploaded by that user, but it only displays the first image. when I click next, no image is displayed. Infact, the div id="EXHIBITION"><img src="/Dinamic uni/Unicycle/www.unicyclepics.co.uk/uploaded_files/1155128419-image.jpg" alt=" "></div>
part of the html is gone...

Help!

bokeh
08-10-2006, 02:13 PM
It the minute you are trying to retrofit the code to do tasks that it wasn't written to do... that's why I said at the start to think about your logic before writing any code. To go forward from here your best bet is to write the code again from scratch taking into consideration all the features the end product requires.