Jump to content

If statements with MySQL?


Nymphetamine

Recommended Posts

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! :D

Sorry if its not clear enough, I'll try and explain better if not.

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.