...

View Full Version : Need a little help with conditions (if)



sommerst
05-11-2009, 04:57 PM
Hello,

I'm trying to set up a display for a database, everything works well except that I would like to check a particular thing before I display the data (to see if it is approved).


<?php
// Fetch the results from the database.
while ($Row = mysql_fetch_array ($Result)) {

echo '<div class=virtualpage hidepiece>';
for ($i = 4; $i < 13; $i++) {
if ( $i == 4 ) {
print "<b>Organization Name:</b> " . " <TD>$Row[$i]</TD>\n";
print ("<br>");
}
elseif ($i == 5 ) {
print "<b>Position Title:</b> " . " <TD>$Row[$i]</TD>\n";
print ("<br>");
}
elseif ($i == 6 ) {
print "<b>Position Type:</b> " . " <TD>$Row[$i]</TD>\n";
print ("<br>");
}
elseif ($i == 7 ) {
print "<b>Start Date (DD-MM-YYYY):</b> " . " <TD>$Row[$i]</TD>\n";
print ("<br>");
}
elseif ($i == 8 ) {
print "<b>End Date (DD-MM-YYYY):</b> " . " <TD>$Row[$i]</TD>\n";
print ("<br>");
}
elseif ($i == 9 ) {
print "<b>Information URL:</b> " . "<a href = $Row[$i]> <TD>$Row[$i]</TD>\n </a>";
print ("<br>");
}
elseif ($i == 10 ) {
print "<b>Contact Email:</b> " . " <TD>$Row[$i]</TD>\n";
print ("<br>");
}
elseif ($i == 11 ) {
print "<b>Position Description:</b> " . " <TD>$Row[$i]</TD>\n";
print ("<br>");
}
else
{
print "<TD>$Row[$i]</TD>\n";
print ("<br>");
}
}

echo '</div>';
}


mysql_close ($Link);


?>

That is my code and it displays everything well.

What I need to do is check:

If $i (at 13) is = 1 then run the code (above), if not, then move to the next.



I tried

$i = 13;

Then tried an if loop that just encompassed the rest of the code, but I got errors (unexpected }, unexpected else, etc)



Thanks for the help

TS

Len Whistler
05-11-2009, 05:30 PM
What I need to do is check:

If $i (at 13) is = 1 then run the code (above), if not, then move to the next.

I tried

$i = 13;



I don't fully understand your question because the value of $i is from 4 to 12 only, not 1 or 13.


---------

Fumigator
05-11-2009, 05:40 PM
Did you know that mysql_fetch_array() also returns an associative array? You don't need to loop through the $Row array at all. Instead of



if ( $i == 4 ) {
print "<b>Organization Name:</b> " . " <TD>$Row[$i]</TD>\n";
print ("<br>");
}


Just do this (I don't know the names of your columns so you'll have to fill that in):


print "<b>Organization Name:</b> " . " <TD>$Row['org_name']</TD><br>\n";


Heck, for that matter, since with your code you know that when $i = 4 that represents organization name, you could also just code it like this:



print "<b>Organization Name:</b> " . " <TD>$Row[4]</TD><br>\n";


That's really not a very good way to do it though, since if you change the list of columns you are selecting in your query, your code breaks.

As for your actual question, can you perhaps explain how you are determining if the data is approved?

sommerst
05-11-2009, 05:47 PM
Sure.

This information is submitted via a form, which is placed in the database.

I have an invisible box which represents whether this particular submission has been approved (it would be number 13 in the list).

So basically I just need to check, if the data at 13 (which is data at $i = 13) is 1 then I want the code to run, if it's not, then it's not an approved submission so I want it to move to the next one.

Len - I realize it steps from 4-12, because that is all I want displayed (the other information is not useful to the public). However, like I've just mentioned, if 13 = 1 then I want it to display, otherwise I want it to move onto the next submission. But I see what you mean.. the reason this may not be working is because I've attempted to use $i (which is the stepper) before it's started to function.. This means I need to pull off row 13 and check it before running the code using another method..

Fumigator - I'll switch that, I actually only need "$Row[$i]" - I removed the other stuff a while back and just haven't gotten around to cleaning it up. Hm, your other suggestion may be the answer to my problem; I'm just wondering if I'll be able to pull the information using the names like that. Let me find out..

Update: Switched the code to:
print "<b>Organization Name:</b> " . " $Row[OrgName]<br>"; and it did not pull the data (once I switched it to OrgName instead of $i it came up blank :(

Update2: I noticed I didn't have the code the exact same way, so when I did


[quote] print "<b>Organization Name:</b> " . " $Row['OrgName']<br>";

I got


Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home3/cyaajcco/public_html/volunteering/thenew/index.php on line 111


Thanks

I solved the problem - I'll clean up the code and post what I did in a min

TS

sommerst
05-11-2009, 06:09 PM
Okay, here is how it works:


<?php
// Fetch the results from the database.
while ($Row = mysql_fetch_array ($Result)) {

if ( $Row[12] == 1 ) {

echo '<div class=virtualpage hidepiece>';
for ($i = 4; $i < 12; $i++) {
if ( $i == 4 ) {
print "<b>Organization Name:</b> " . " $Row[$i]<br>";
}
elseif ($i == 5 ) {
print "<b>Position Title:</b> " . " $Row[$i]<br>";
}
elseif ($i == 6 ) {
print "<b>Position Type:</b> " . " $Row[$i]<br>";
}
elseif ($i == 7 ) {
print "<b>Start Date (DD-MM-YYYY):</b> " . " $Row[$i]<br>";
}
elseif ($i == 8 ) {
print "<b>End Date (DD-MM-YYYY):</b> " . " $Row[$i]<br>";
}
elseif ($i == 9 ) {
print "<b>Information URL:</b> " . "<a href = $Row[$i]> $Row[$i]<br> </a>";
}
elseif ($i == 10 ) {
print "<b>Contact Email:</b> " . " $Row[$i]<br>";
}
elseif ($i == 11 ) {
print "<b>Position Description:</b> " . " $Row[$i]<br>";
}
else
{
print "$Row[$i]<br>";
}
}

echo '</div>';
}

}
mysql_close ($Link);


?>

So basically I just pulled the information (which was at row 12, my apologies, I didn't realize I had it as checking until it was smaller with <) with


if ( $Row[12] == 1 ) {

Surrounded the whole thing (it gave me errors at first, but that was dealt with easily)

And now it works wonderfully.

Thanks for your help!

TS

Fumigator
05-11-2009, 06:30 PM
For future reference, you can get around that "unexpected T_ENCAPSED_AND_WHITESPACE" error by enclosing the variable in squiggly brackets (which I failed to do in my snippet):



print "<b>Organization Name:</b> " . " {$Row['OrgName']}<br>";


And again I'll just nag you a bit to use the column names instead of numbers... it's just more resistant to breaking in case the query changes.

sommerst
05-12-2009, 04:07 AM
Alright, I'll give that a shot.

Thanks again!

TS



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum