Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 05-04-2009, 04:27 PM Looping Issues
Average Talker

Posts: 18
Name: Jon
Location: Southern California
Trades: 0
I have a dynamic number of "category_names.table2, optionvalue.table2" in my query.

These "category_names" are joined with the dynamic "type.table1, name.table1, street.table1, city.table1, state.table1, zip.table1, phone.table1."

The type.table1 & optionvalue.table2 are the common between the 2 tables. The type and optionvalue are always a letter and a number (i.e.) 'K1', 'K2', etc. but the number of them is not always the same. There could be 1 or 20.

This code will echo them exactly as I have instructed, but it's not what I need.

Code:
 $result = mysql_query($query) or print mysql_error();
$num_results = mysql_num_rows($result);
for ($i=0; $i < $num_results; $i++)
while ($row = mysql_fetch_array($result))
 {
  $type=$row["type"];
  $name=$row["name"];
  $street=$row["street"];
  $city=$row["city"];
  $state=$row["state"];
  $zip=$row["zip"];
  $phone=$row["phone"];
  $category_name=$row["category_name"];
 
 echo "$num_results<br>";
 echo  "<u>$category_name</u><br>";
 echo "$name<br>" .
  "$street<br>" .
  "$city, $state $zip<br>" . 
  "$phone<br>";
}
Produces the following....

173

Italian Restaurants
Joe's Pizza
1234 Sauce Street
Pizzatown, NY 90000
(212) 555-1212

Italian Restaurants
Cicilia's Italian Restaurant
333 S. Cheese Lane
Lasagna, IL 95000
(432) 555-1212

Chinese Restaurants
Fortune Cookie Heaven
4321 Chow Mein Rd.
Chopstix, CA 92001
(310) 555-1212

Chinese Restaurants
Wang Ling Palace
4444 Beef Broccoli Way
Pork, TX 92001
(414) 555-1212

With all 173 results echoing respectively below their "$category_name."

What I need is..

Italian Restaurants
Joe's Pizza
1234 Sauce Street
Pizzatown, NY 90000
(212) 555-1212

Cicilia's Restaurant
333 S. Cheese Lane
Lasagna, IL 95000
(432) 555-1212

Chinese Restaurants
Fortune Cookie Heaven
4321 Chow Mein Rd.
Chopstix, CA 92001
(310) 555-1212

Wang Ling Palace
4444 Beef Broccoli Way
Pork, TX 92001
(414) 555-1212


I have tried moving the echo outside the loop, however I ONLY get the final record.

I have tried mixing code - exiting php, listing the category in HTML then re-entering php, but the same thing happens.

I have tried several other rudimentary ideas by searching the WWW, and reading "PHP & MySql" - (Luke Willing & Laura Thompson) as well as "Learning PHP & MySql" (Michele Davis & John Phillips). I have been able to follow various coding ideas which ultimately parse properly and echo results, but CANNOT get the category_names to echo ONCE with their respective names, street, etc... below the categories.

Can anyone POINT me in the right direction - I don't need anyone to DO this for me exactly -> however some guidance is requested as I am clearly not getting it.

I do beleive that what I need is a multi-dimensional array but I just can't wrap my head around creating it using dynamic $variables.

Thanks.
LoganKonlan is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-04-2009, 06:02 PM Re: Looping Issues
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
Can anyone POINT me in the right direction
Simple, save the last category parsed by the loop.
If the next loop iteration finds the same value, then do nothing.
If the value have changed, display it.

I'd prototype it like this:
PHP Code:
<?php
$prevCat
="";
$q="select type, name, address from table";
$r=mysql_query($q);
while(
$row=mysql_fetch_array($r)){
  if (
$row['type']!=$prevCat){
    echo 
"<div class="sectionHeader">{$row['type']}</div>";
  }
  
$prevCat=$row['type'];
  
/*
  ...
  ...
  Continue your script
  ...
  */
}
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 05-05-2009, 06:37 PM Re: Looping Issues
Average Talker

Posts: 18
Name: Jon
Location: Southern California
Trades: 0
Tripy,
What a simple answer. I have spent so much time on this and your solution worked in 5 minutes.
The things I could accomplish with your brain! Thank you once again for your help - it is greatly appreciated and here's what I did.

Code:
 
$prevCat="";
$query = "select name, type, street, city, state, zip, phone FROM table...."
$result = mysql_query($query) or print mysql_error();
$num_results = mysql_num_rows($result);
for ($i=0; $i < $num_results; $i++)
while ($row = mysql_fetch_array($result))
{
$type=$row["type"];
$name=$row["name"];
$street=$row["street"];
$city=$row["city"];
$state=$row["state"];
$zip=$row["zip"];
$phone=$row["phone"];
$category_name=$row["category_name"];
 
echo "$num_results<br>";
 
if ($type != $prevCat)
 
echo "<u>$category_name</u><br>";
 
$prevCat=$type;
 
echo "$name<br>" .
"$street<br>" .
"$city, $state $zip<br>" . 
"$phone<br>";
}

Last edited by LoganKonlan; 05-05-2009 at 06:50 PM.. Reason: error
LoganKonlan is offline
Reply With Quote
View Public Profile
 
Old 05-06-2009, 01:45 AM Re: Looping Issues
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
I have spent so much time on this and your solution worked in 5 minutes.
The things I could accomplish with your brain!
Thanks, but it's just simple logic, pseudo-code and experience...
Try to break down each part of your script that gives you a problem in smaller parts, and you'll end up with a solution for all of them.

What I like to do, is to draw the logic on paper.
Take a pencil, a sheet, and draw an box for each logical operations.
Link them with arrows. I make a diamond for an if, with 2 arrow for the yes/no path, and connect them to the logical operations.
It's a tremendous help when you seem stuck, to be able to draw your thoughts. And when you want to validate a request done by a customer, it's very easy for him to pinpoint an error in the flow.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Looping Issues
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.22822 seconds with 12 queries