Jump to content

looping variable


simeonC
Go to solution Solved by ginerjm,

Recommended Posts

Im trying to loop a variable to extract each customer out the database however it just repeats the same customer

 

any help would be greatly appreciated.

$completed_orders=mysql_query("SELECT * FROM repair_orders JOIN (serviced_vechicles,Customers) ON repair_orders.car_id=serviced_vechicles.car_id AND Customers.customer_id=serviced_vechicles.customer_id WHERE repair_status='0' ORDER BY customer_name DESC");
$existingCustomers=mysql_num_rows($completed_orders);
echo $existingCustomers;
$fetch_complete=mysql_fetch_array($completed_orders);
$customer=$fetch_complete;

$requested_customers = 
'<a href="">';
'<tr>
<td>'.$customer['customer_name'].'</td>
<td>'.$customer['year'].'</td>
<td>'.$customer['make'].'</td><
td>'.$customer['model'].'</td>
<td>'.$customer['date'].'<td>
</tr>';
'</a>';

How do I loop through ^^^^^ $requested_customers

Link to comment
Share on other sites

You use a while loop around your mysql_fetch_array code.

while ($customer = mysql_fetch_array($completed_orders)){
   $requested_customers = 
   '<a href="">
   <tr>
   <td>'.$customer['customer_name'].'</td>
   <td>'.$customer['year'].'</td>
   <td>'.$customer['make'].'</td>
   <td>'.$customer['model'].'</td>
   <td>'.$customer['date'].'<td>
   </tr>
   </a>';
   

   //Do something with $requested_customers
}
Note that your HTML is not valid either, you cannot put an <a> tag around a table row. You'd need to link each cell separately. Edited by kicken
Link to comment
Share on other sites

I ended up linking each cell seperately that such a bother hopefully we can get that updated.. however I tried the while loop and the do while loop I am still getting 1 row output..

I used the mysql_num_rows function and it counts two rows for the result set. So now what do I do. Should I be using the mysqli extension. I am really lost

Link to comment
Share on other sites

Still having issues with my while{} loop and do{} while() loop I want it to run an output of different row for each result set sent  back from the mysql database. However it is remaining stagnent with same result even though mysql_num_rows function shows two result sets set back. Now I am lost here is the code again please help me.

<?php session_start();?>
<?php 
$con=mysql_connect('localhost','root','root');
$db_select=mysql_select_db('total',$con);


$completed_orders=mysql_query("SELECT * FROM repair_orders JOIN (serviced_vechicles,Customers) ON repair_orders.car_id=serviced_vechicles.car_id AND Customers.customer_id=serviced_vechicles.customer_id WHERE repair_status='0' ORDER BY customer_name DESC");
$existingCustomers=mysql_num_rows($completed_orders);
if ($existingCustomers<1){ $message= 'You do not have any complete vehicles in your shop';}else{

	$fetch_complete=mysql_fetch_array($completed_orders);
	
 do{
   $requested_customers = 
   '<tr>
   <td>'.$customer['customer_name'].'</td>
   <td>'.$customer['year'].'</td>
   <td>'.$customer['make'].'</td>
   <td>'.$customer['model'].'</td>
   <td>'.$customer['date'].'<td>
   </tr>';
   

   //Do something with $requested_customers
}while ($customer = mysql_fetch_array($completed_orders));
    
}

	
	
	
	
?>

Link to comment
Share on other sites

You use a while loop, not a do/while loop. Start with the code I gave you above and then extend it. You do not need your mysql_num_rows line or that initial $fetch_customer line. The while loop would begin just after your mysql_query call.

Link to comment
Share on other sites

while ($completed_customers = MySQL_fetch_array($completed_orders)

{

   (build your heml)

}

echo </table>

echo your accumulated html

 

BTW - in your loop you are re-initializing $requested_customers every time, so you are probably only seeing the very last customer anyway.

 

s/b "$requested_customers .=  blah blah blah;

Link to comment
Share on other sites

Thank you I love this forum I used the dot equals before but I kept re-initializing the variable.

as suchVVVVVVV

 while ($customer = mysql_fetch_array($completed_orders)){
   $requested_customers = '<tr>';
   $requested_customers .='<td>'.$customer['customer_name'].'</td>';
   $requested_customers .='<td>'.$customer['year'].'</td>';
   $requested_customers .='<td>'.$customer['make'].'</td>';
   $requested_customers .='<td>'.$customer['model'].'</td>';
   $requested_customers .='<td>'.$customer['date'].'<td>';
   $requested_customers .='</tr>';
   

   //Do something with $requested_customers
}

VVVCorrect way for anyone with similiar problem novice mistake use dot equal from beginningVVV

 while ($customer = mysql_fetch_array($completed_orders)){
   $requested_customers .= '<tr>';
   $requested_customers .='<td>'.$customer['customer_name'].'</td>';
   $requested_customers .='<td>'.$customer['year'].'</td>';
   $requested_customers .='<td>'.$customer['make'].'</td>';
   $requested_customers .='<td>'.$customer['model'].'</td>';
   $requested_customers .='<td>'.$customer['date'].'<td>';
   $requested_customers .='</tr>';
   

   //Do something with $requested_customers
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.