Jump to content

confused, Im gettinbg double entries in text file


bakhtn

Recommended Posts

    //if any items in order table then display all items in order table

 

$order_sql = "select * from orders";

$order_result = $handle -> query( $order_sql );

 

if($order_result->num_rows)

{

 

echo '<table bgcolor=#FFFF66 ><td>';

    while ($order = $order_result -> fetch_assoc())

{

 

$category_sql = "select category from category where category_id = {$order['category_id']}";

$category_result = $handle -> query( $category_sql );

$category_name = $category_result -> fetch_row();

 

$menu_sql = "select item_name from menu where item_id = {$order['item_id']}";

$menu_result = $handle -> query( $menu_sql );

$menu_name = $menu_result -> fetch_row();

 

echo $category_name[0] . ': ' . $menu_name[0] . ' ' . $order['size'] . ' ' . $order['type'] . ' ' . $order['amount'] . '<br />';

 

//insert data into order.txt file for print

$file_insert = $category_name[0] . ": " . $menu_name[0] . " " . $order['size'] . " " . $order['type'] . " " . $order['amount'] . "\r\n";

@ $fp = fopen( 'order.txt', 'ab' );

 

if($fp)

{

fwrite( $fp, $file_insert, strlen( $file_insert ) );

}

else

{

echo 'could not insert data into order.txt file for print, CLICK ON NEW ORDER';

}

fclose($fp);

Link to comment
Share on other sites

//if any items in order table then display all items in order table
$order_sql = "SELECT o.size, o.type, o.amount, c.category, m.item_name
              FROM orders   AS o
              JOIN category AS c USING(category_id)
              JOIN menu     AS m USING(item_id)";
$order_result = $handle->query($order_sql);
   
if($order_result->num_rows)
{
    echo "<table bgcolor='#FFFF66'>\n";
    echo "<tr>\n";
    echo "<th></th>\n";
    echo "<th></th>\n";
    echo "<th></th>\n";
    echo "<th></th>\n";
    echo "<th></th>\n";
    echo "<\tr>\n";

    while ($order = $order_result -> fetch_assoc())
    {
        echo "<tr>\n";
        echo "<td>$order['category']}</td>\n";
        echo "<td>{$order['item_name']}</td>\n"; 
        echo "<td>{$order['size']}</td>\n";
        echo "<td>{$order['type']}</td>\n";
        echo "<td>{$order['amount']}</td>\n";
        echo "</tr>\n";
        //Append insert data to write into order.txt file
        $file_insert .="{$order['category']}: {$order['item_name']} {$order['size']} {$order['type']} {$order['amount']}\r\n";
    }
    echo "</table>\n";

    @$fp = fopen( 'order.txt', 'ab' );
    if($fp)
    {
        fwrite($fp, $file_insert, strlen($file_insert) );
        fclose($fp);
    }
    else
    {
        echo 'could not insert data into order.txt file for print, CLICK ON NEW ORDER';
    }
}

Link to comment
Share on other sites

I needed to have the sql statements with in the loop, I needed one row for each row of data , its a relational database. and the while loop does close in the actual script, Ive found a way round it, but why does this code produce a duplicate data in the file, and not when outputted to the browser. I just want to know why.

Link to comment
Share on other sites

I needed to have the sql statements with in the loop, I needed one row for each row of data , its a relational database.

 

No you do not need to run the query within the loop. The whole point of a relational database is that you can 'relate' the data, for example, using a JOIN. Your initial code was terribly inefficient.

 

1. Again, no need to run those queries in a loop. Simply do ONE query with appropriate JOINs to get all the data you need.

2. Do not open/close the file within the while loop. Just open it once, write your data, then close it one.

 

I really don't see why your original code would write duplicates, but suspect it might have something to do with opening/closing the file repeatedly. If it isn't hat then you just might have duplicate data.

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.