473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to query and output data in a nested loop?

7
I'm trying to implement a function which will print all all the diferent type of products and their makes. I have 3 types of products and 6 makes. The problem is, it only prints one type of product 1 with the different makes, and then doesnt loop again to print the other types.


Here is my code:

Expand|Select|Wrap|Line Numbers
  1. function search_product($types, $makes, $orgProd, $orgName){
  2.  
  3.  
  4. require_once ('../mysqli_connect.php');
  5.  
  6.     $makesId=count($makes); 
  7.     $typesId=count($types); 
  8.  
  9. echo '<form  action="proccessIt.php"  method="post" > ';
  10.  
  11. echo'<fieldset>';
  12.  
  13.  
  14.  
  15.  
  16.  for($i=0; $i<$typesId; $i++){
  17.  
  18.      echo ' type_i: '. $type=$types[$i];    
  19.  
  20.  
  21.  
  22.                                 for($i=0; $i<$makesId; $i++){
  23.                                      $make=$makes[$i];    
  24.  
  25.  
  26.         //print product type as header 
  27.  
  28.  
  29.         // Retrieve product model and  id:
  30.  
  31.         echo $q = "SELECT model AS  product_model, id_product AS id_product  FROM product WHERE id_brand=$make AND  id_type=$type;";
  32.  
  33.  
  34.         $r =mysqli_query ($dbc, $q) or die(mysqli_error($dbc)); // Run the query.
  35.  
  36.  
  37.  
  38.         if ($r) { // If it ran OK, display the records.
  39.  
  40.  
  41.  
  42.                 echo'    <table align="center" cellspacing="3" cellpadding="3" width="75%">
  43.                 <tr><td align="left"><b><div id="prodName"> '.$typeLoop.' </div><br/></b></td></tr>
  44.                 ';
  45.  
  46.                 // Fetch and print all the records:
  47.                 while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
  48.  
  49.  
  50.  
  51.                 echo '<tr>  <td align="left">'  .  
  52.                 ' <input type="checkbox" name="checkbox[]checkbox2[]" value="'. $row['id_product'] .'" value="'. $row['product_model']  .'">
  53.                 '. $row['product_model'] .'  
  54.                    </b>    </td>   </tr>
  55.                 ';
  56.  
  57.                 }//end of while
  58.  
  59.  
  60.         mysqli_free_result ($r); // Free up the resources.    
  61.  
  62.         } else { // If it did not run OK.
  63.  
  64.         // Public message:
  65.         echo '<p class="error">The current request could not be retrieved. We apologize for any inconvenience.</p>';
  66.  
  67.         // Debugging message:
  68.         echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';
  69.  
  70.  
  71.  
  72.         } // End of else.
  73.  
  74.  
  75.  
  76.  
  77.         }//end of second for loop
  78.     }//end of for loop
  79.  
  80.     echo '<tr>  <td align="left">'  . 
  81.                 '<input type="hidden" name="orgProd"  value="'.$orgProd.'" />
  82.                   </td>   </tr>
  83.                 ';
  84.  
  85.  
  86.                 echo '<tr>  <td align="left">'  . 
  87.                 '<input type="hidden" name="orgName"  value="'.$orgName.'" />
  88.                   </td>   </tr>
  89.                 ';
  90.  
  91.  
  92. echo '
  93.               </table>
  94.               <input type="submit" value="Search">
  95.               <input type="hidden" name="submitte" value="7"/>
  96.  
  97.               </fieldset>
  98.               </form>'; // Close the table.
  99.  
  100. mysqli_close($dbc); // Close the database connection.
  101.  
  102.  
  103.  
  104. }//end of product function
Feb 5 '11 #1
3 1866
HaLo2FrEeEk
404 256MB
Use foreach instead.

Expand|Select|Wrap|Line Numbers
  1. foreach($types as $type) {
  2.   foreach($makes as $make) {
  3.     // You've now got $make and $type as variables
  4.     }
  5.   }
It'll loop through each item in the array. So for each type you'll loop through the entire make array.
Feb 6 '11 #2
Yess
7
Hi Halo2FrEeEk,

I only just seen your reply. Already test it and it works!

Thanks you very much, it had already taken me to long to get it to work... I cant beleive it was so simple!
Feb 7 '11 #3
nathj
938 Expert 512MB
The foreach is by far the best way to get this working. But for future reference the us of $i in both the inner and outer loop is bad parctice. I recommend using $i for the outer and $j for the inner if you want to use short variable names. Alternatively call them proper names - if the loops get deep it'll make maintenance easier.

Cheers
nathj
Feb 12 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
0
by: John Wilson | last post by:
Hello, I have the following code which populates as table data from a SQL Server 2000 stored proc (RSByDemoID2). Below that is the view and stored procedure which takes @DemoID as input to match...
2
by: smauldin | last post by:
Why does the execution plan have a nested loop join for a simple select with an UDF in the where clause? Here is the query: select * from test_plan where vCol = my_udf('test') Here is the...
5
by: Blankdraw | last post by:
I can't get this nested loop to break the outer loop at the 5th data value so control can proceed to the next array col and continue pigeon-holing the next 5 in its own column. Why can I not get...
8
by: GeorgeSmiley | last post by:
Does anyone know of a way, via VBA, to set the screen position of query results to a particular top, left position? I've glanced at API techniques but cannot find exactly what will do the trick....
1
by: Goldie | last post by:
Can anyone offer advice on how to do a nested loop with vb I need the loop nested in the main loop to be passed a variable from the parent loop for SQL purposes. eg: parent selects all...
12
by: upernikaw | last post by:
Hello, I am attempting to create a nested loop (in Access 2003/VB) that will print a report for a set of user defined months inputed on a form and that will print out for every Client. So the first...
1
by: Phil | last post by:
Is it possible to swap rows and columns in select query output so that each record's data is displayed in a column? I want to collect data each day and display it in a query with each day's date...
8
by: TobbeK | last post by:
Need some help with a nested loop. This one (my example code below) prints out a correct 3 column HTML table with the recordset with proper opening and closing HTML tags AS LONG as the GRUOP BY...
1
by: reachravi70 | last post by:
Hello Users, I am new to PERL. There is a requirement to write a PERL script to query the Oracle data based on the user input date parameter and write the output to Excel. I have the following...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.