Page 1 of 1
Seems Simple...Can't Make it Work!
Posted: Fri Apr 09, 2004 8:05 pm
by PHP_Ryan
Need some help...I have the following code:
-----------------------------------------------------
$sql = "SELECT DISTINCT cat, subcat, subcat_id FROM products ORDER BY cat, subcat";
$result = mysql_query($sql);
$last_cat = "";
while ($row = mysql_fetch_array($result))
{
if ($row['cat'] !== $last_cat)
{
echo "\"{$row['cat']}*\"\n";
$last_cat = $row['cat'];
}
echo "\"results.php?tunedsearch={$row['subcat_id']}|{$row['subcat']}#\" +\n";
}
-----------------------------------------------------
I cannot get the ouput to look like this:
-----------------------------------------------------
"Wheels & More*results.php?tunedsearch=aluminumwheels|Aluminum Wheels#" +
"results.php?tunedsearch=plasticwheels|Plastic Wheels#" +
"results.php?tunedsearch=dishwheels|Dish Wheels",
"Nitro Trucks*results.php?tunedsearch=stadiumtrucks|Stadium Trucks#" +
"results.php?tunedsearch=monstertrucks|Monster Trucks#"+
"results.php?tunedsearch=buggies|Buggies",
-----------------------------------------------------
My DB is set up in the following manor:
-----------------------------------------------------
columns: cat, subcat, subcat_id
example row: Wheels & More, Aluminum Wheels, aluminumwheels
-----------------------------------------------------
Any help would be greatly appreciated...thanks in advance,
Ryan
Posted: Fri Apr 09, 2004 8:48 pm
by feyd
Code: Select all
$sql = "SELECT DISTINCT cat, subcat, subcat_id FROM products ORDER BY cat, subcat";
$result = mysql_query($sql);
$buf = "";
$last_cat = "";
while ($row = mysql_fetch_array($result))
{
if ($rowї'cat'] !== $last_cat)
{
$buf = preg_replace("/#"\s+$/","",",$buf);
$buf .= ""{$rowї'cat']}*"\n";
$last_cat = $rowї'cat'];
}
$buf .= ""results.php?tunedsearch={$rowї'subcat_id']}|{$rowї'subcat']}#" +\n";
}
I think might work...
edit: made a quick change to fix the regex.
Posted: Fri Apr 09, 2004 9:01 pm
by PHP_Ryan
Hmmm, I can see what you've modified, but it isn't returning any results. Is there a way for it to output the results?
Posted: Fri Apr 09, 2004 9:06 pm
by feyd
echo $buf;
Posted: Fri Apr 09, 2004 9:19 pm
by PHP_Ryan
I am getting the following output from the above code you have delivered:
Single cat, subcat output:
"Wheels & More*"
"results.php?tunedsearch=aluminumwheels|Aluminum Wheels#" +
"results.php?tunedsearch=offroadtires|Off-Road Tires#" +
"results.php?tunedsearch=onroadtires|On-Road Tires#" +
"results.php?tunedsearch=plastic|Plastic#" +
"results.php?tunedsearch=suspension|Suspension#" +
I think the reason it is not working is that the first line for each cat must have the subcat data follow. Then, each subcat below that. The last subcat cannot end with a '+' but a ','
Single cat, subcat example:
"Wheels & More*results.php?tunedsearch=aluminumwheels|Aluminum Wheels#" +
"results.php?tunedsearch=plasticwheels|Plastic Wheels#" +
"results.php?tunedsearch=dishwheels|Dish Wheels",
Thank you so much for putting your time into this.
Posted: Fri Apr 09, 2004 10:10 pm
by feyd
Code: Select all
$sql = "SELECT DISTINCT cat, subcat, subcat_id FROM products ORDER BY cat, subcat";
$result = mysql_query($sql);
$buf = "";
$last_cat = "";
while ($row = mysql_fetch_array($result))
{
if ($rowї'cat'] !== $last_cat)
{
$buf = preg_replace("/#"\s\+$/","",",$buf);
$buf .= ""{$rowї'cat']}*"\n";
$last_cat = $rowї'cat'];
}
$buf .= ""results.php?tunedsearch={$rowї'subcat_id']}|{$rowї'subcat']}#" +\n";
}
Messed up the regex... forgot + was a special wildcard...
Posted: Fri Apr 09, 2004 10:17 pm
by PHP_Ryan
Soooo close! The only thing I can see that is making it not work is that the very first line of a category outputs:
"Bodies*"
"results.php?tunedsearch=monstertrucks|Monster Trucks#" +
(the rest of the subcategories)...
Instead of:
"Bodies*results.php?tunedsearch=monstertrucks|Monster Trucks#" +
(the rest of the subcategories)...
AND the very last line outputted needs to have ); in place of the ,
Thank you again!
Posted: Fri Apr 09, 2004 10:39 pm
by feyd
Code: Select all
$sql = "SELECT DISTINCT cat, subcat, subcat_id FROM products ORDER BY cat, subcat";
$result = mysql_query($sql);
$buf = "";
$last_cat = "";
while ($row = mysql_fetch_array($result))
{
if ($rowї'cat'] !== $last_cat)
{
$buf = preg_replace("/#"\s\+$/","",",$buf);
$buf .= ""{$rowї'cat']}*";
$last_cat = $rowї'cat'];
}
$buf .= ""results.php?tunedsearch={$rowї'subcat_id']}|{$rowї'subcat']}#" +\n";
}
$buf = preg_replace("/,$/",";",$buf);
Posted: Fri Apr 09, 2004 11:02 pm
by PHP_Ryan
I'm sorry to keep putting you through this...for some reason it's just missing something extremely small...the last code you sent gives me:
"Bodies*"results.php?tunedsearch=monstertrucks|Monster Trucks#" +
"results.php?tunedsearch=stadiumtrucks|Stadium Trucks",
"Chassis & More*"results.php?tunedsearch=aluminum|Aluminum",
"Nitro Trucks*"results.php?tunedsearch=monstertrucks|Monster Trucks#" +
"results.php?tunedsearch=stadiumtrucks|Stadium Trucks",
"Wheels & More*"results.php?tunedsearch=aluminumwheels|Aluminum Wheels#" +
"results.php?tunedsearch=offroadtires|Off-Road Tires#" +
"results.php?tunedsearch=onroadtires|On-Road Tires#" +
"results.php?tunedsearch=plastic|Plastic#" +
"results.php?tunedsearch=suspension|Suspension#" +
It has to be exactly like this:
"Bodies*results.php?tunedsearch=monstertrucks|Monster Trucks#" +
"results.php?tunedsearch=stadiumtrucks|Stadium Trucks",
"Chassis & More*results.php?tunedsearch=aluminum|Aluminum",
"Nitro Trucks*"results.php?tunedsearch=monstertrucks|Monster Trucks#" +
"results.php?tunedsearch=stadiumtrucks|Stadium Trucks",
"Wheels & More*results.php?tunedsearch=aluminumwheels|Aluminum Wheels#" +
"results.php?tunedsearch=offroadtires|Off-Road Tires#" +
"results.php?tunedsearch=onroadtires|On-Road Tires#" +
"results.php?tunedsearch=plastic|Plastic#" +
"results.php?tunedsearch=suspension|Suspension");
1. On the category line, there should be no " after the *
2. The very last line of code outputted needs to display ); instead of +
Everything else is right on the money!
p.s. I hope I'm not buggin' to bad...thanks again! - Ryan
Posted: Fri Apr 09, 2004 11:20 pm
by feyd
Code: Select all
$sql = "SELECT DISTINCT cat, subcat, subcat_id FROM products ORDER BY cat, subcat";
$result = mysql_query($sql);
$buf = "";
$last_cat = "";
while ($row = mysql_fetch_array($result))
{
if ($rowї'cat'] !== $last_cat)
{
$buf = preg_replace("/#"\s\+$/","",",$buf);
$buf .= ""{$rowї'cat']}*";
$last_cat = $rowї'cat'];
}
$buf .= "results.php?tunedsearch={$rowї'subcat_id']}|{$rowї'subcat']}#" +\n";
}
$buf = preg_replace("/"\s\+$/",");",$buf);
Posted: Sat Apr 10, 2004 1:40 am
by PHP_Ryan
You got it!!! Except for one tiny thing...
At the beginning of each results.php?... there needs to be a " except for the one after the cat*
On the last line of the output the # needs to be a " before the );
Everything else is perfect!
Thank you so much!!!
Posted: Sat Apr 10, 2004 1:53 am
by feyd
Code: Select all
$sql = "SELECT DISTINCT cat, subcat, subcat_id FROM products ORDER BY cat, subcat";
$result = mysql_query($sql);
$buf = "";
$last_cat = "";
while ($row = mysql_fetch_array($result))
{
if ($rowї'cat'] !== $last_cat)
{
$buf = preg_replace("/#"\s\+$/","",",$buf);
$buf .= ""{$rowї'cat']}*";
$last_cat = $rowї'cat'];
}
else
{
$buf .= '"';
}
$buf .= "results.php?tunedsearch={$rowї'subcat_id']}|{$rowї'subcat']}#" +\n";
}
$buf = preg_replace("/"\s\+$/",");",$buf);
Posted: Sat Apr 10, 2004 1:56 am
by PHP_Ryan
This now does everything I need it too except put the " in fornt of the result.php? after the cat line* (see example for the --> where the " needs to be)
Example Output:
"Wheels & More*results.php?tunedsearch=aluminumwheels|Aluminum Wheels#" +
-->results.php?tunedsearch=offroadtires|Off-Road Tires#" +
-->results.php?tunedsearch=onroadtires|On-Road Tires#" +
-->results.php?tunedsearch=plastic|Plastic#" +
-->results.php?tunedsearch=suspension|Suspension");
Code: Select all
<?php
$sql = "SELECT DISTINCT cat, subcat, subcat_id FROM products ORDER BY cat, subcat";
$result = mysql_query($sql);
$buf = "";
$last_cat = "";
while ($row = mysql_fetch_array($result))
{
if ($rowї'cat'] !== $last_cat)
{
$buf = preg_replace("/#"\s\+$/","",",$buf);
$buf .= ""{$rowї'cat']}*";
$last_cat = $rowї'cat'];
}
$buf .= "results.php?tunedsearch={$rowї'subcat_id']}|{$rowї'subcat']}#" +\n";
}
$buf = preg_replace("/#"\s\+$/","");",$buf);
echo $buf;
?>
Posted: Sat Apr 10, 2004 1:59 am
by feyd
check the code I just posted ^^
Posted: Sat Apr 10, 2004 2:06 am
by PHP_Ryan
Got it, and that did it! All I had to do was make a tiny change from:
$buf = preg_replace("/\"\s\+$/",");",$buf);
to this:
$buf = preg_replace("/#\"\s\+$/","\");",$buf);
Thank you so much for your help...I can now have a relaxing easter.
BTW: The site is under heavy construction, but it was for the category/subcategory search on my Nitro R/C website:
http://www.infiniterc.com/tunedsearch.php
All searching worked except for that, and to me it was most important!