Seems Simple...Can't Make it Work!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PHP_Ryan
Forum Newbie
Posts: 12
Joined: Fri Apr 09, 2004 8:05 pm

Seems Simple...Can't Make it Work!

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
PHP_Ryan
Forum Newbie
Posts: 12
Joined: Fri Apr 09, 2004 8:05 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

echo $buf;
PHP_Ryan
Forum Newbie
Posts: 12
Joined: Fri Apr 09, 2004 8:05 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
PHP_Ryan
Forum Newbie
Posts: 12
Joined: Fri Apr 09, 2004 8:05 pm

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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);
PHP_Ryan
Forum Newbie
Posts: 12
Joined: Fri Apr 09, 2004 8:05 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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);
PHP_Ryan
Forum Newbie
Posts: 12
Joined: Fri Apr 09, 2004 8:05 pm

Post 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!!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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);
PHP_Ryan
Forum Newbie
Posts: 12
Joined: Fri Apr 09, 2004 8:05 pm

Post 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)) 
&#123; 
if ($row&#1111;'cat'] !== $last_cat) 
&#123; 
$buf = preg_replace("/#"\s\+$/","",",$buf); 
$buf .= ""&#123;$row&#1111;'cat']&#125;*"; 
$last_cat = $row&#1111;'cat']; 
&#125; 
$buf .= "results.php?tunedsearch=&#123;$row&#1111;'subcat_id']&#125;|&#123;$row&#1111;'subcat']&#125;#" +\n"; 
&#125; 
$buf = preg_replace("/#"\s\+$/","");",$buf);
echo $buf;
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

check the code I just posted ^^
PHP_Ryan
Forum Newbie
Posts: 12
Joined: Fri Apr 09, 2004 8:05 pm

Post 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!
Post Reply