Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

echo variable only on lowest number

12:24 pm on Jan 3, 2011 (gmt 0)

Hi people of Webmaster World, atb for 2011.

In summary I'm inserting form data into mysql whereby one req number can have 3 item lines. DB schema is each item line also has the req number.

I'm trying to echo out the contents of a variable but only on the lowest item number. Code is:

$reqquery = "SELECT * FROM `porders` WHERE `username` = '$username' AND `type` = 'request' ";
$userreqs = mysql_query($reqquery) or die(mysql_error());

while ($row = mysql_fetch_row($userreqs)) {
$reqno = $row[6]; $item = $row[8];

if ($item <= 1) {
echo $reqno;

echo $item;

The above only echos out the $reqno if the item number is 1.

The problem I've got is another page has the following select.

$reqquery = "SELECT * FROM `porders` WHERE `username` = '$username' AND `type` = 'request' AND `status` = 'rejected' ";
$userreqs = mysql_query($reqquery) or die(mysql_error());

And because I've stipulated rejected and some rejected reqs are not item 1 it doesn't echo out the $reqno.

Its like I need to say:
if $reqno = $reqno echo reqno on lowest item number.

I've been thinking down the following angles but I can't get anything to work.
// if item 1 is present echo $reqno.
// if item 2 is present echo $reqno else echo $reqno on lowest item number.
// if item 3 is present echo $reqno else echo $reqno on lowest item number.

// echo $reqno on lowest item number only.

Any help would be much appreciated.
Cheers O.
1:57 pm on Jan 3, 2011 (gmt 0)

Or perhaps something like

// It needs to know how many item numbers for a $reqno then echo out the $reqno on the lowest item number present.

$fixed_array = array('$row[6]', '$row[8]');

foreach($fixed_array as $reqno => $item) {
if ($item == '1') {
echo $reqno;
elseif ($item == '2') {
echo $reqno;
elseif ($item == '3') {
echo $reqno;

2:49 pm on Jan 3, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

while ($row = mysql_fetch_row($userreqs)) {
$reqno = $row[6]; $item = $row[8];

if ($item <= 1) {
echo $reqno;

echo $item;

I think this is your problem. I'm struggling to understand quite what you're trying to do here, but I think you want to echo $reqno only on the first row retrieved from the database?

So, I think something like this should work:

$i = 0;
while ($row = mysql_fetch_row($userreqs)) {
$reqno = $row[6];
$item = $row[8];

if ($i < 0) {
echo $reqno;

echo $item;
3:42 pm on Jan 3, 2011 (gmt 0)

Hi Readie,

Thanks for the reply and I apologise for my first explanation, I was struggling to document it clearly.

Your example does work and echos out the $reqno against the appropriate item no if I change echo $item, to echo $reqno; but that's my problem.

I only want it to echo the $reqno once per 3 items displayed. If all 3 items are rejected I want it to echo the $reqno once and at item 1. If item 1 was pending I need it to display item 2 and item 3 with the $reqno being displayed at item 2 only and item 3 blank.

Initially when a req is raised it can have 1 item, 2 or 3. When I insert it I create a row for each item in the db with the same req number. It looks like:

reqno1 - item1 - rejected
reqno2 - item1 - pending
reqno2 - item2 - rejected
reqno3 - item1 - rejected
reqno3 - item2 - pending
reqno3 - item3 - rejected

When I stipulate rejected I need to display:

For reqno1
req number and item1

For reqno2
req number and item2

For reqno3
req number and item1
No Number and item3

At present for example on reqno3 I can get it to echo out the req number for both items but thats my problem. I only want it to echo it out once and the No Number above be blank.

Hopefully a bit clearer.

edited 16.02 No Number and item2 to No Number and item3. Item 3 sorry.

If you need me to start from scratch with trying to explain it happy to do so.

May help, I've copied the following from the actual web page display.

$reqno $item

REQ123 1

REQZebra1 2

REQZebra1 3

REQ12345 2

REQ99 3

REQZebra1 is shown on item 2 and 3 and i only want it to show on item 2.

Featured Threads

Hot Threads This Week

Hot Threads This Month