Nymphetamine Posted August 21, 2008 Share Posted August 21, 2008 Hi there guys, I'm building a chart, where all along the top there's the weeks in a year, and down the side there's a small list of properties. I've got myself a MySQL database with all the right fields. This is a bit hard to explain... I'll start with the first thing I need. I can't quite figure out how to do something like this: if ($row['week']) = "week1" && ($row['property']) = "1" { echo "<td>Property 1 has been booked for this week</td>"; } else { echo "<td>Property 1 is empty</td>"; } Basically, I want all the cells in the chart to be either booked or empty, depending on the data in my database... I hope that's clear enough... Would there be anyway to automate this haha? As there's 300+ cells to fill. Furthermore... I want the user to be able to click on a cell (whether booked or not), it will come up with a new page, and the user will fill in or change the details for that particular week and property. Any help would be great! Sorry if its not clear enough, I'll try and explain better if not. Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/ Share on other sites More sharing options...
trq Posted August 21, 2008 Share Posted August 21, 2008 = is an assignment operator, your looking for == which is a comparison operator. if ($row['week']) == "week1" && ($row['property']) == "1" { As for automating it, we need to see some code. Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621577 Share on other sites More sharing options...
Nymphetamine Posted August 21, 2008 Author Share Posted August 21, 2008 Hmmm thanks for that, Though it comes up with "Unexpected T_IS_EQUAL" Any ideas? Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621603 Share on other sites More sharing options...
Nymphetamine Posted August 21, 2008 Author Share Posted August 21, 2008 My code thus far... <?php mysql_connect("######", "######", "######") or die(mysql_error()); mysql_select_db("######") or die(mysql_error()); ?> html stuff <?php $result = mysql_query("SELECT * FROM #######"); while($row = mysql_fetch_assoc($result)){ if ($row['date']) == "0501" && ($row['property']) == "1" { echo "<tr><td>Booked</td>"; } else { echo "<tr><td>Not booked</td>"; } ?> Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621604 Share on other sites More sharing options...
trq Posted August 21, 2008 Share Posted August 21, 2008 Sorry, didn't even see that. if ($row['date'] == 0501 && $row['property'] == 1) { Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621615 Share on other sites More sharing options...
Nymphetamine Posted August 21, 2008 Author Share Posted August 21, 2008 Right, no more error, But it comes up with "Not booked" when it should be coming up with "Booked" I have the values "0501" under date and "1" under property in the database, and in the same row. There must be something wrong with my code Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621628 Share on other sites More sharing options...
trq Posted August 21, 2008 Share Posted August 21, 2008 Sorry, we should probably treat them as strings. if ($row['date'] == '0501' && $row['property'] == '1') { Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621629 Share on other sites More sharing options...
Nymphetamine Posted August 21, 2008 Author Share Posted August 21, 2008 Thanks, Now I'm trying to replace "Booked" with the name value of that row... echo "<tr><td>($row['name'])</td>"; ...doesnt seem to work. >.< Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621732 Share on other sites More sharing options...
mmarif4u Posted August 21, 2008 Share Posted August 21, 2008 try this: echo "<tr><td>$row['name']</td>"; OR echo "<tr><td>$row[name]</td>"; Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621737 Share on other sites More sharing options...
Nymphetamine Posted August 21, 2008 Author Share Posted August 21, 2008 Thanks buddy, echo "<tr><td>$row[name]</td>"; ...works. However now its coming up with the name AND "Not booked" haha. I think its because I put some more data in the database. <?php $result = mysql_query("SELECT * FROM ########"); while($row = mysql_fetch_assoc($result)){ if ($row['date'] == '0501' && $row['property'] == '1') { echo "<tr><td>$row[name]</td>"; } else { echo "<tr><td>Empty</td>"; } if ($row['date'] == '1201' && $row['property'] == '1') { echo "<td>$row[name]</td>"; } else { echo "<td>Empty</td>"; } } ?> Must I use elseif or something? Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621747 Share on other sites More sharing options...
mmarif4u Posted August 21, 2008 Share Posted August 21, 2008 If you want to show multiple data in a page, then what you write is OK. but if not, then better to use elseif statement. Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621751 Share on other sites More sharing options...
Nymphetamine Posted August 21, 2008 Author Share Posted August 21, 2008 Okies, Something like? <?php $result = mysql_query("SELECT * FROM ########"); while($row = mysql_fetch_assoc($result)){ if ($row['date'] == '0501' && $row['property'] == '1') { echo "<tr><td>$row[name]</td>"; } elseif ($row['date'] == false && $row['property'] == false) { echo "<tr><td>Empty</td>"; } if ($row['date'] == '1201' && $row['property'] == '1') { echo "<td>$row[name]</td>"; } elseif ($row['date'] == false && $row['property'] == false) { echo "<td>Empty</td>"; } } ?> Bearing in mind that that row wouldn't even be there... Hmm that can't be it haha. Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621756 Share on other sites More sharing options...
mmarif4u Posted August 21, 2008 Share Posted August 21, 2008 while($row = mysql_fetch_assoc($result)){ if ($row['date'] == '0501' && $row['property'] == '1') { echo "<tr><td>$row[name]</td>"; } elseif ($row['date'] == false && $row['property'] == false) { echo "<tr><td>Empty</td>"; } if ($row['date'] == '1201' && $row['property'] == '1') { echo "<td>$row[name]</td>"; } elseif ($row['date'] == false && $row['property'] == false) { echo "<td>Empty</td>"; } } If you want to use single if for other values also,then no need to use elseif.but if you wan to show one row,then it should be: while($row = mysql_fetch_assoc($result)){ if ($row['date'] == '0501' && $row['property'] == '1') { echo "<tr><td>$row[name]</td>"; } elseif ($row['date'] == false && $row['property'] == false) { echo "<tr><td>Empty</td>"; } elseif ($row['date'] == '1201' && $row['property'] == '1') { echo "<td>$row[name]</td>"; } elseif ($row['date'] == false && $row['property'] == false) { echo "<td>Empty</td>"; } else { echo "<td>No record found</td>"; } } Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621759 Share on other sites More sharing options...
Nymphetamine Posted August 21, 2008 Author Share Posted August 21, 2008 Hmm that works if the name and date are there, But I need it to show a cell with "Empty" in it if it can't find those values... Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621771 Share on other sites More sharing options...
mmarif4u Posted August 21, 2008 Share Posted August 21, 2008 then its better to just: if(){ do this } else { do that } Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621773 Share on other sites More sharing options...
Nymphetamine Posted August 21, 2008 Author Share Posted August 21, 2008 But the trouble is when I do that it displays the names and "Empty" .... Also for some reason they are appearing back to front. Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621776 Share on other sites More sharing options...
Nymphetamine Posted August 21, 2008 Author Share Posted August 21, 2008 Here's my code so far guys... <?php $result = mysql_query("SELECT * FROM ##########"); while($row = mysql_fetch_assoc($result)){ echo "<td>"; if ($row['date'] == '0501' && $row['property'] == '1') { echo "$row[name]"; } elseif ($row['date'] == false && $row['property'] == false) { echo "Empty"; } echo "</td><td>"; if ($row['date'] == '1201' && $row['property'] == '1') { echo "$row[name]"; } elseif ($row['date'] == false && $row['property'] == false) { echo "Empty"; } echo "</td><td>"; if ($row['date'] == '1901' && $row['property'] == '1') { echo "$row[name]"; } elseif ($row['date'] == false && $row['property'] == false) { echo "Empty"; } echo "</td>"; } ?> It doesn't work though, it displays the second name before the first one, which I can't have! As for the third, it displays nothing, since it doesn't exist in the database, but I need it to display a cell with "Empty" in it. Thanks for the help so far guys! Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621814 Share on other sites More sharing options...
mmarif4u Posted August 21, 2008 Share Posted August 21, 2008 What false is representing, and try else instead of elseif. Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621819 Share on other sites More sharing options...
Nymphetamine Posted August 21, 2008 Author Share Posted August 21, 2008 Nah lol else shows the names aswell as "Empty" Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621824 Share on other sites More sharing options...
mmarif4u Posted August 21, 2008 Share Posted August 21, 2008 elseif ($row['date'] == false && $row['property'] == false) false? Is it from table. Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621826 Share on other sites More sharing options...
Nymphetamine Posted August 21, 2008 Author Share Posted August 21, 2008 Well I said earlier in the thread that I thought that wouldnt work. The row for that date and property wouldnt exist. Is there another way I can do it? For instance something like... echo "Empty" unless ($row['date'] == '0501' && $row['property'] == '1') Haha how I wish unless actually existed. Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621831 Share on other sites More sharing options...
mmarif4u Posted August 21, 2008 Share Posted August 21, 2008 Did you tried something like this: if ($row['date'] == '0501' && $row['property'] == '1') { echo "$row[name]"; } else{ echo "Empty"; } Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621832 Share on other sites More sharing options...
Nymphetamine Posted August 21, 2008 Author Share Posted August 21, 2008 Haha yes Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621834 Share on other sites More sharing options...
mmarif4u Posted August 21, 2008 Share Posted August 21, 2008 I am not sure, whats wrong there. but i am sure that if else will work which i post just now. Because if, if condition is false, it will print else condition. try it again. Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621838 Share on other sites More sharing options...
Nymphetamine Posted August 21, 2008 Author Share Posted August 21, 2008 Like I said before, it echoes everything, I think because theres multiple entries in the database. Comes up like this: Empty Empty Name 2 Name 1 When it should be: Name 1 Name 2 or Empty Empty or etc. Link to comment https://forums.phpfreaks.com/topic/120628-if-statements-with-mysql/#findComment-621840 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.