Click to See Complete Forum and Search --> : [RESOLVED] need help to make a tag query


izlik
10-31-2007, 08:40 PM
Hello there, i can a script for file uploading wich have an infinished tag system i wanna try to get working since the developer apperently dont.

The system is finished in the way that you can set tags and they show, but when you press a tag it gives a 404 error. when you press a tagg you get redirected to http://mypage.com/tags.php?tag=thetagiclicked

now i wonder, what mysql query is it i need to put in tags.php inorder for the tag system to search trough my whole database and show items with the tag i have clicked?

i hope someone can help me with this, i would be very very very much thankfull.

izlik
11-05-2007, 06:51 PM
noone that got any idea? :/

jasonahoule
11-05-2007, 06:55 PM
You will need to post some code and the database schema or at least the table you want to search. You are asking for help but not providing any detail.

izlik
11-07-2007, 06:12 AM
You will need to post some code and the database schema or at least the table you want to search. You are asking for help but not providing any detail.

im sorry, the database is named "frog" and the table is named "images" and bellow is one of the lines from the database.

and it's in the following order

id, file, name, uid, witdth, height, size, ip, extra, time, views, bandwith, lastview, server id, deleted, votes, ratings, private, upload_session, tags

1342, '4ac375da.jpg', 'ep2_outland_030012.jpg', 10, 1024, 768, 185731, 'xx.xxx.6.121', '6916e225b43f03c6b13792f33bb70acb', 1192124119, 31, 5776072, 1193604860, 1, 0, 0, 0, 0, '15904a1a2f4f', 'Half-Life2,Half-Life,Games'

jasonahoule
11-08-2007, 05:02 PM
You want to make sure you build an index on the tags field otherwise you may put a lot of strain on you database if the images table gets to be pretty large. Your SQL would look something like this...

SELECT * FROM `images`
WHERE `tags` LIKE '%Half-Life%';

izlik
11-13-2007, 04:35 PM
You want to make sure you build an index on the tags field otherwise you may put a lot of strain on you database if the images table gets to be pretty large. Your SQL would look something like this...

SELECT * FROM `images`
WHERE `tags` LIKE '%Half-Life%';



ye, so much ive understood. but what i have a problem with is that if someone click a diffrent tags let's say "cars" ? the query above is for halflife only as i se it, what i need is someting that works for the tags that is clicked. like if someone clicks "cars" it searches for all images with that tag. if they click "tigers", the script in tags.php will search for that

jasonahoule
11-14-2007, 10:25 AM
So your query would need to get the value from the query string


$query = "SELECT * FROM `images`WHERE `tags` LIKE '%" . $_GET['tag'] . "%'";

izlik
11-14-2007, 06:02 PM
So your query would need to get the value from the query string


$query = "SELECT * FROM `images`WHERE `tags` LIKE '%" . $_GET['tag'] . "%'";


someting like this or am i off?

<center><br>

<?php
$con = mysql_connect("localhost","database","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("database", $con);
$result = mysql_query("SELECT * SELECT * FROM `images` WHERE `tags` LIKE '%" . $_GET['tag'] . "%'"");
while($row = mysql_fetch_array($result))
{
echo '<a href="http://mydomain.com/out.php/'.$row['name'].'</a>';';
echo "<br />";
}
?>

trying to make it eco every image with the tag that was pressed.

jasonahoule
11-15-2007, 08:35 PM
Did you try it?

izlik
11-16-2007, 06:20 AM
Did you try it?

ye, trows me the error as seen bellow :(

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/izlik/public_html/tags.php on line 12

chazzy
11-16-2007, 07:39 AM
This line is clearly incorrect.


$result = mysql_query("SELECT * SELECT * FROM `images` WHERE `tags` LIKE '%" . $_GET['tag'] . "%'"");


Should be


$result = mysql_query("SELECT * FROM `images` WHERE `tags` LIKE '%" . $_GET['tag'] . "%'"");

izlik
11-16-2007, 07:46 AM
This line is clearly incorrect.


$result = mysql_query("SELECT * SELECT * FROM `images` WHERE `tags` LIKE '%" . $_GET['tag'] . "%'"");


Should be


$result = mysql_query("SELECT * FROM `images` WHERE `tags` LIKE '%" . $_GET['tag'] . "%'"");


ah... but then i got this error after changing the error.

Parse error: syntax error, unexpected '"' in /home/izlik/public_html/tags.php on line 11

jasonahoule
11-16-2007, 03:28 PM
That would be just as it says. There is an unexpected " in there. Try this (notice the number of quotes at the end...)

$result = mysql_query("SELECT * FROM `images` WHERE `tags` LIKE '%" . _GET['tag'] . "%'");

izlik
11-16-2007, 04:40 PM
That would be just as it says. There is an unexpected " in there. Try this (notice the number of quotes at the end...)

$result = mysql_query("SELECT * FROM `images` WHERE `tags` LIKE '%" . _GET['tag'] . "%");


when i fixed that it yields

Parse error: syntax error, unexpected T_STRING in /home/rizzler/public_html/tags.php on line 11

<center><br>
mysql_error()
<?php
$con = mysql_connect("localhost","asd","asd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("das", $con);
$result = mysql_query("SELECT * FROM `images` WHERE `tags` LIKE "%' . _GET['tag'] . '%");
while($row = mysql_fetch_array($result))
{
echo '<a href='http://mydomain.com/out.php/'.$row['name'].'</a>';
echo "<br/>";
}
?>

izlik
11-16-2007, 07:45 PM
i got it to work now, thanks both of you for the help ! :)

However, there is 1 last thing i need help with that i cant get my head around at all :( in the code bellow they are atm showing 1 picture per line, how can i make the stack 4 pictures per line?

example:
1 2 3 4
1 2 3 4
1 2 3 4
etc.

<?php

$con = mysql_connect("localhost","asd","das") OR die('Could not connect: ' . mysql_error());
mysql_select_db("asd", $con);

$result = mysql_query("
SELECT *
FROM `images`
WHERE `tags`
LIKE '%" . mysql_real_escape_string($_GET['tag']) . "%'
ORDER BY views DESC LIMIT 50
")
OR die(mysql_error());

while($row = mysql_fetch_array($result))
{

echo '<a href="http://mydomain.net/show.php/' .$row['id'].'_' .$row['name'].'"><img src="http://www.mydomain.net/out.php/t' .$row['id'].'_' .$row['name'].'"></a>';
echo "<br/>\n";
}

?>

jasonahoule
11-16-2007, 10:08 PM
Put them in their own <div> blocks and float them.

echo '<div style="float:left;width:25%"><a href="http://mydomain.net/show.php/' .$row['id'].'_' .$row['name'].'"><img src="http://www.mydomain.net/out.php/t' .$row['id'].'_' .$row['name'].'"></a></div>';

izlik
11-17-2007, 08:22 AM
Put them in their own <div> blocks and float them.

echo '<div style="float:left;width:25%"><a href="http://mydomain.net/show.php/' .$row['id'].'_' .$row['name'].'"><img src="http://www.mydomain.net/out.php/t' .$row['id'].'_' .$row['name'].'"></a></div>';


ah, that worked except for 1 little problem. as you can se in the image bellow, the second line of images always comes with 3 empety spots for every tag i try now, and the footer always get's overun by images from the left side :/

jasonahoule
11-17-2007, 01:03 PM
You are going to have to learn some CSS to accomplish what you are trying to do. You will need to clear the floats to fix the problem with the footer. Add this line right after the code that writes out your images.

<div style="clear:both"></div>

izlik
11-21-2007, 02:53 PM
You are going to have to learn some CSS to accomplish what you are trying to do. You will need to clear the floats to fix the problem with the footer. Add this line right after the code that writes out your images.

<div style="clear:both"></div>


hey again jason, thanks to your advice i have gotten the script to work with 1 last problem.

if you go to http://filefrog.net/new.php?&s=300 and press the tag "games" and then go to the bottom of the page and press "next" like usually :) the codes switch pages likes it should, but shows the same images on all pages,

do you know why? bellow is my updated code.

<?
include "includes/inc.php";

require_once("header.php");

$template->set_filenames(array(
'body' => 'tags.html')
);

?>

<?php

$con = mysql_connect("localhost","asd","das) OR die('Could not connect: ' . mysql_error());
mysql_select_db("asd", $con);

//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}

//Edit $result to be your query
$result = mysql_query("
SELECT *
FROM `images`
WHERE `tags`
LIKE '%" . mysql_real_escape_string($_GET['tag']) . "%'
ORDER BY views
$max
")
OR die(mysql_error());
$rows = mysql_num_rows($result);

//This is the number of results displayed per page
$page_rows = 30;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

while($row = mysql_fetch_array($result))
{

echo '<div style="float:left;width:25%"><a href="http://filefrog.net/show.php/' .$row['id'].'_' .$row['name'].'"><img src="http://www.filefrog.net/out.php/t' .$row['id'].'_' .$row['name'].'"></a></div>';
echo "<br>\n";

}
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
?>
<div style="clear:both"></div>

<?
// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1&amp;tag={$_GET['tag']}'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous&amp;tag={$_GET['tag']}'> <-Previous</a> ";
}

//just a spacer
echo " ---- ";

if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next&amp;tag={$_GET['tag']}'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last&amp;tag={$_GET['tag']}'>Last ->></a> ";
}
?>
<?
include "footer.php";
?>

jasonahoule
11-21-2007, 03:17 PM
Ok, so you want to display 30 results per page. You need to tell your query that. You already have a $page_rows variable defined. Move that above your query. You are already passing a page number too. You just need to multiply your page minus one times your page_rows. Change your query to something like this

$max = $page_rows * ($pagenum - 1);
$result = mysql_query("
SELECT *
FROM `images`
WHERE `tags` LIKE '%". mysql_real_escape_string('$_GET['tag'] ."%'
ORDER BY views
LIMIT $max, $page_rows");

I don't know where you were getting that $max variable from or how you were using it but here it is telling MySQL where to start in the results. The $page_rows is how many results to fetch.

izlik
11-21-2007, 03:39 PM
Ok, so you want to display 30 results per page. You need to tell your query that. You already have a $page_rows variable defined. Move that above your query. You are already passing a page number too. You just need to multiply your page minus one times your page_rows. Change your query to something like this

$max = $page_rows * ($pagenum - 1);
$result = mysql_query("
SELECT *
FROM `images`
WHERE `tags` LIKE '%". mysql_real_escape_string('$_GET['tag'] ."%'
ORDER BY views
LIMIT $max, $page_rows");

I don't know where you were getting that $max variable from or how you were using it but here it is telling MySQL where to start in the results. The $page_rows is how many results to fetch.

aha, i edited that, now i get
Parse error: syntax error, unexpected T_STRING in /home/izlik/public_html/tags.php on line 28

and that line is WHERE `tags` LIKE '%". mysql_real_escape_string('$_GET['tag'] ."%' in Crimson Editor

jasonahoule
11-21-2007, 03:55 PM
typo:
('$_GET['tag'] ."%'
should be
('%".$_GET['tag']."%'

izlik
11-21-2007, 04:18 PM
typo:
('$_GET['tag'] ."%'
should be
('%".$_GET['tag']."%'

still getting the exact same error on the same line :/

jasonahoule
11-21-2007, 04:27 PM
Sorry, did it again

WHERE `tags` LIKE '%". mysql_real_escape_string($_GET['tag']) ."%'

izlik
11-21-2007, 05:23 PM
Sorry, did it again

WHERE `tags` LIKE '%". mysql_real_escape_string($_GET['tag']) ."%'

ahh thanks, 3 other errors popped up wich i fixed, now another 1 came up that i could not fix, can you se it? updated code bellow.

Parse error: syntax error, unexpected T_WHILE in /home/izlik/public_html/tags.php on line 32

<?php

$con = mysql_connect("localhost","asd","dasd") OR die('Could not connect: ' . mysql_error());
mysql_select_db("asd", $con);

//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}

//Edit $result to be your query
$max = $page_rows * ($pagenum - 1);
$result = mysql_query("
SELECT *
FROM `images`
WHERE `tags` LIKE '%". mysql_real_escape_string($_GET['tag']) ."%'
ORDER BY `views`
LIMIT $max, $page_rows OR die(mysql_error());
")
while($row = mysql_fetch_array($result));

$rows = mysql_num_rows($result);

//This is the number of results displayed per page
$page_rows = 30;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

echo '<div style="float:left;width:25%"><a href="http://filefrog.net/show.php/' .$row['id'].'_' .$row['name'].'"><img src="http://www.filefrog.net/out.php/t' .$row['id'].'_' .$row['name'].'"></a></div>';
echo "<br>\n";


?>
<div style="clear:both"></div>

<?
// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1&amp;tag={$_GET['tag']}'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous&amp;tag={$_GET['tag']}'> <-Previous</a> ";
}

//just a spacer
echo " ---- ";

if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next&amp;tag={$_GET['tag']}'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last&amp;tag={$_GET['tag']}'>Last ->></a> ";
}

?>

jasonahoule
11-21-2007, 06:42 PM
Look at where your closing quote and parentheses are at. Below is the corrected.


$result = mysql_query("
SELECT *
FROM `images`
WHERE `tags` LIKE '%". mysql_real_escape_string($_GET['tag']) ."%'
ORDER BY `views`
LIMIT $max, $page_rows") OR die(mysql_error());

izlik
11-21-2007, 08:49 PM
Look at where your closing quote and parentheses are at. Below is the corrected.


$result = mysql_query("
SELECT *
FROM `images`
WHERE `tags` LIKE '%". mysql_real_escape_string($_GET['tag']) ."%'
ORDER BY `views`
LIMIT $max, $page_rows") OR die(mysql_error());


I have now solved the problem! :) http://filefrog.net/new.php?&s=300 and click the tags! ;)

and thanks to your guidence! marking this topic as resolved and thank you very very very much jason, i owe you one bigtime! :)

jasonahoule
11-21-2007, 08:52 PM
No problem. I am happy to help.

ai3rules
12-03-2007, 12:35 PM
I have the exact same question, only I want to do this in a stored procedure instead of from the php page.

Is that possible?

Something like:


DELIMITER $$

DROP PROCEDURE IF EXISTS `MOTD`.`listMOTD_Connection_Logs` $$
CREATE PROCEDURE `listMOTD_Connection_Logs`(s varchar(50))
BEGIN
set @s = s;
select * from MOTD_Connection_Logs where System_Name like '%@s%' order by Creation_Date;
END $$

DELIMITER ;

I can't figure out how to include the @s variable along with the wildcards.

Any suggestions?