Jump to content

[SOLVED] Simple Function Problem


shayster

Recommended Posts

Okay first of all I am new and this is my first function. I borrowed most of the code but then tried to change it to suit my needs. The function is suppose to receive an id and then use it in the function to join two tables and then display the results in a table. I get the Table and the field names but can't get the results from the table. I added a line to print the id to assure the function was getting it and it was. I also ran the query through mysql directly and got the proper results. Thanks in advance:

 

function orderidToEditOrder($orderid){
  //given a table name, generates HTML table including
  //edit buttons

  $orderid = filter_input(INPUT_POST, "orderid");
  $orderid = mysql_real_escape_string($orderid);

  global $dbConn;
  $output = "";
  $query = "select category.cat_name, orders.wgt_ounces, orders.del_date from orders, category where orders.id='$ordersid' and category.id=orders.cat_id";

  $result = mysql_query($query, $dbConn);

  $output .= "<table border = '1'>\n";
  //get column headings

  //get field names
  $output .= "<tr>\n";
  while ($field = mysql_fetch_field($result)){
    $output .= "  <th>$field->name</th>\n";
  } // end while
//add empty columns for edit
  $output .= "<th width=70></th>\n";
  $output .= "</tr>\n\n";

  //get row data as an associative array


while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){

    echo "<tr>\n";

$output .= "  <td>Item: {$row['category.cat_name']} </td>";
$output .= "<td>Weight in Ounces: {$row['orders.wgt_ounces']} </td>";
$output .= "<td>Delivery Date: {$row['orders.del_date']} </td></tr>";



}
$output .="</table>";
return $output;

} // end orderidToEditOrder

Link to comment
Share on other sites

You are using $results twice. You can't do that the way you are doing it. Arrays have a pointer that points at the 'current' element of the array. So after the first while loop, the pointer is at the end of the array, so it finds no values for the second loop.

 

Before your second loop, try this code:

 

reset($result);

 

and see what happens.

Link to comment
Share on other sites

Ahh, sorry my bad. Query results aren't arrays, so that won't work.

 

Ok, there are two ways I have done this in the past, but I'm not sure which one is better.

 

1) re-query the database. This gives you a fresh result that you can run your while loop on. Pro - it works. Con - requires an extra query to the database.

2) add an extra while loop after the first query, where you assign the results of the query to an array. Then, in your while loop, use that array instead of the query results. Pro - it works. Con - php intensive. Could take a long time for large result sets.

 

I'll be honest, I don't know which one is better, nor do I know if there is a better way than either of those. But they will both work.

 

 

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.