0

I'm trying to print multiple mysql rows, using mysql_fetch_array with while loop but it only prints the first result while my table contains many of them with the right conditions

$queryString="SELECT * FROM items WHERE order_id='".$order_id."' and confirm_order='0' ORDER BY id";
$myquery=mysql_query($queryString);

$handle = printer_open("POS");
printer_start_doc($handle, "My Document");
printer_start_page($handle);
$font = printer_create_font("Arial", 35, 20, 300, false, false, false, 0);
printer_select_font($handle, $font);

while ($fetch = mysql_fetch_array($myquery)) {

    $product=$fetch[product_name];
    $type=$fetch[type];
    $q=$fetch[item_quantity];
    $comment=$fetch[comment];

    $tex="".$q." ".$type." ".$comment." ".$product."";
    printer_draw_text($handle, $tex, 10, 10);
  }
printer_delete_font($font);
printer_end_page($handle);
printer_end_doc($handle);
printer_close($handle);

Note:- and I can't use mysqli or PDO as I'm just testing something on an old project

11
  • 1
    $fetch[product_name] should be $fetch['product_name'] (this is a side remark, it doesn't answer the question.) Commented Jan 16, 2018 at 11:21
  • 1
    To find errors and warnings, put error_reporting(E_ALL|E_NOTICE); in front of the script Commented Jan 16, 2018 at 11:23
  • 1
    $product=$fetch[product_name]; $type=$fetch[type]; $q=$fetch[item_quantity]; $comment=$fetch[comment]; Need to be:- $product=$fetch['product_name']; $type=$fetch['type']; $q=$fetch['item_quantity']; $comment=$fetch['comment'];(this is a side remark, it doesn't answer the question.) Commented Jan 16, 2018 at 11:24
  • @AlivetoDie — No, they don't. It's a best practise, but not a need. Commented Jan 16, 2018 at 11:26
  • I can't see any reason that the loop wouldn't work. Try looking at the count or getting the code to output plain text in case it is an issue with your TeX library. Commented Jan 16, 2018 at 11:28

1 Answer 1

2

Based on printer_draw_text Manual

The function draws text at position x, y using the selected font.

In your code x,y values are 10,10. So each time new text written on the same postion. That means previous one over-written by new-one and hense only single value is drawn.

There are two possible solution:-

1.Either Change x,y position values after each iteration.

$counter = 10; //add a number counter

while ($fetch = mysql_fetch_assoc($myquery)) {//use assoc for lighter array iteration

    $product=$fetch['product_name']; //use quotes around indexes, best practise
    $type=$fetch['type'];
    $q=$fetch['item_quantity'];
    $comment=$fetch['comment'];

    $tex="$q $type $comment $product";//remove unncessary quotes
    printer_draw_text($handle, $tex, $counter, $counter); // use that number counter as x,y position
    $counter+10;//in each iteration chnage the value by adding 10
}

2.Or Create new pages in each iteration:-

while ($fetch = mysql_fetch_assoc($myquery)) {//use assoc for lighter array iteration

    $product=$fetch['product_name'];//use quotes around indexes, best practise
    $type=$fetch['type'];
    $q=$fetch['item_quantity'];
    $comment=$fetch['comment'];

    $tex="$q $type $comment $product";//remove unncessary quotes
    printer_draw_text($handle, $tex, 10, 10);
    printer_end_page($handle); //end page on each iteration
  }
printer_delete_font($font);
printer_end_doc($handle);
printer_close($handle);

Note:- Add rest of the code as it is.

Sign up to request clarification or add additional context in comments.

1 Comment

@SophieBernard glad to help you :):)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.