Forum Moderators: coopster

Message Too Old, No Replies

help with logic loop

         

Bigjohn

6:54 pm on Feb 28, 2004 (gmt 0)

10+ Year Member



Hi guys - it's me again...

I have some working code:


while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($row['itemstatus'] == "1") {$avail='<div id="buttons"><p>
<a href="buyform.php?item='.$row['item'].'&class='.$class.'&desc=' .$row['itemname'].'&price='.$row['itemprice'].'" target="_blank">BUY</a></p>
<p><a href="#" onclick="sendAnnotatedMailTo(\'sales\',\'mcdonoughfineart\',\'com\','. '\''.$row['itemname'].' inquiry\');">Inquire</a></p></div>'; $row_style="stock";}
else {$avail="HOLD"; $row_style="holditem";}

Notice the test on 'itemstatus'.

Now I've decided that I need 3 states for that variable: Available, HOLD, and SOLD. (1,0,2).

After this IF statement is resolved the rest of the WHERE loop builds a table row for display. How do I make the function SKIP building the row when status of the item is SOLD?

Thanks in advance!

John

[edited by: jatar_k at 6:59 pm (utc) on Feb. 28, 2004]
[edit reason] fixed sidescroll [/edit]

jatar_k

6:59 pm on Feb 28, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about just changing

if ($row['itemstatus'] == "1")
to
if ($row['itemstatus']!= "2")

Bigjohn

7:31 pm on Feb 28, 2004 (gmt 0)

10+ Year Member



I still do condition sensitive things based on zero or 1.

If zero, the row is built using a CSS class that displays it differently and the word HOLD is printed where the buttons would be.

If 1, the table row is built and buttons are included to allow a user to buy or inquire about the item.

Should I post or attach the whole code?

John

jatar_k

7:44 pm on Feb 28, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



then this

if ($row['itemstatus'] == "1" && $row['itemstatus']!= "2")

and switch your else to

else if ($row['itemstatus'] == "0")

Bigjohn

8:18 pm on Feb 28, 2004 (gmt 0)

10+ Year Member



How does that stop it from completing the where loop and building the table row?

jatar_k

8:45 pm on Feb 28, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



How does that stop it from completing the where loop and building the table row?

I don't get the question.

You want to complete the whole loop I would think. Actually i was a little confused there. You don't need the check for!=2 at all just the addition of else if instead of plain if.

As it stood in your original you tested for 1 and did something special for it and everything else was processed in your else.

If you want to process 1 one way
process 0 another way
and not process 2

then you need to explicitly handle 1 and 0 and not do anything if you have a 2.

therefore
if ($row['itemstatus'] == "1") {
// do stuff for AVAILABLE status
} else if ($row['itemstatus'] == "0") {
// do stuff for HOLD status
}

and we ignored SOLD altogether

Bigjohn

9:04 pm on Feb 28, 2004 (gmt 0)

10+ Year Member



I guess I was not clear.

The current code tests for 0 or 1. If 1, it builds buttons (you see that in the code I posted), if Zero it sets the button variable to the word HOLD which is echoed into the HTML.

After the tests, the where loop builds the table rows. Currently it builds the table row either with the buttons (itemstatus=1) or without (itemstatus=0).

I want to NOT BUILD A ROW if the status is SOLD (itemstatus=2). - basically just increment the loop.

I think I might have to enclose the table building part in another IF.

I thought about doing:


while($row = mysql_fetch_array($result, MYSQL_ASSOC) AND $row['itemstatus']!= "2")


but that would seem to me that it would END the while loop the first time it got a 2.

John

mykel79

9:28 pm on Feb 28, 2004 (gmt 0)

10+ Year Member



>I think I might have to enclose the table building part in another IF

Either that, or you could enclose the button building and the table building in an IF. Something like:
if ($row['itemstatus']!="2") {
.
.
all the code you have in the while
}

So if it's SOLD, then the loop will just be incremented. I think that's better than just putting the table building in an IF, because there will be less checking to be done during parsing - if it's a SOLD, then the loop increments right away, without first going through two IF's to see if a button has to be built.

Bigjohn

10:47 pm on Feb 28, 2004 (gmt 0)

10+ Year Member



That did it.

I put the conditional (if!=2) first, after the while loop. and guess what! It worked first time out! I might just be getting the hang of this...

John

Netizen

3:45 pm on Feb 29, 2004 (gmt 0)

10+ Year Member



If I followed that all correctly - why not amend the initial SQL statement to exclude "SOLD" items?

Bigjohn

4:05 pm on Feb 29, 2004 (gmt 0)

10+ Year Member



See my code line in message 7.

It seems to me that that query would END the while loop when it encountered the first SOLD item.

John

willybfriendly

4:23 pm on Feb 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the data is being pulled from a DB it might be easier to tackle the problem there. Just construct your query so that the items that are not to be included are not pulled from the DB.

Something like:

$query = "SELECT * FROM your_table WHERE itemstatus = 'test1' OR itemstatus = 'test2'";

No need to pull data that you aren't going to use. Save on resource use both in the DB access and in the running of the script.

WBF

Netizen

11:22 am on Mar 1, 2004 (gmt 0)

10+ Year Member



I completely agree with this - there is no point in extracting a whole load of data from the database if it isn't going to be used anywhere. (I am an advocate for not using "select *" type queries as often this returns columns which aren't even used).

Bigjohn

11:35 am on Mar 1, 2004 (gmt 0)

10+ Year Member



I'll give willyb's query a try later today!

since the 'itemstatus' will be either 0,1,2, should I query and say!="2"?

SELECT * FROM table WHERE itemstatus!= "2"

Thanks.

Bigjohn

11:20 pm on Mar 1, 2004 (gmt 0)

10+ Year Member



Worked like a charm.

Thanks!