Seems Simple...Can't Make it Work!
Moderator: General Moderators
Seems Simple...Can't Make it Work!
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
-----------------------------------------------------
$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
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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";
}edit: made a quick change to fix the regex.
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.
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.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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";
}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!
"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!
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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);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
"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
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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);- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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);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");
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;
?>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!
$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!