Forum Moderators: coopster

Message Too Old, No Replies

Empty cell in MySQL

         

outdoorxtreme1

3:00 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



How do I output the HTML code   if a cell in MySQL has no data in it?

jatar_k

3:19 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could use an if statement

if there is a value returned echo the value
else
echo ' ';

outdoorxtreme1

3:26 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



I want it to echo the data in the cell if this is any. If there is not any data I want to echo &nbsp

How does this look?

if (!empty($row['miles'])) echo ' ',$row['miles'],;

jatar_k

3:30 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



close but I am assuming you want only $row['miles'] then the non breaking space in the else statement. I may have misunderstood but I was thinking this way

if (!empty($row['miles'])) $row['miles'];
else echo ' ';

directrix

3:44 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



You have to be careful with empty: it returns true for 0 (the integer) and "0" (the string), which may not be what you want!

For a string, you could use strlen:
echo (strlen($row['miles'])) > 0? $row['miles'] : ' ';

Or, if the field might contain trailing blanks:
echo (strlen(rtrim($row['miles']))) > 0? $row['miles'] : ' ';

outdoorxtreme1

3:50 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



It only seem to work on my last row on the HTML table I am trying to create. Here is what I am trying to do:

<TABLE class="tripSelect" cellspacing="1" border="1" id="table1" width="499">
<TR class="tripSelectHeader c4">
<TD width="65" align="center">
<font class="tinytextblk" face="Arial" size="1">Trip Type</font>
</TD>
<TD width="58" align="center">
<font class="tinytextblk" face="Arial" size="1">Trip Location</font>
</TD>
<TD width="131" align="center">
<font class="tinytextblk" face="Arial" size="1">State</font>
</TD>
<TD width="25" align="center">
<font class="tinytextblk" face="Arial" size="1">Date</font>
</TD>
<TD width="25" align="center">
<font class="tinytextblk" face="Arial" size="1">Miles</font>
</TD>
<TD width="52" align="center">
<font class="tinytextblk" face="Arial" size="1">view</font>
</TD>
<TD width="37" align="center">
<font class="tinytextblk" face="Arial" size="1">View</font>
</TD>
<TD width="31" align="center">
<font class="tinytextblk" face="Arial" size="1">View</font>
</TD>
<TD width="33" align="center">
<font class="tinytextblk" face="Arial" size="1">View</font>
</TD>
</TR>

<?
$host = " ";
$user = " ";
$pass = " ";
$dbname = " ";

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);

$sql = 'SELECT * FROM trip_log ORDER BY TSpickYear, TSpickMonth, TSpickDay ASC';
$result = mysql_query($sql) or die ('<p>select died: ' . mysql_error());

while ($row = mysql_fetch_array($result)) {
$startdate = date("F d Y", mktime(0,0,0,$row['TSpickMonth'],$row['TSpickDay'],$row['TSpickYear']));
$enddate = date("F d Y", mktime(0,0,0,$row['TFpickMonth'],$row['TFpickDay'],$row['TFpickYear']));

echo '<TR class="tripSelectRow">';
echo '<TD width="65" align="center">';
echo '<font class="tinytextblk" face="Arial" size="1">',$row['triptype'],'</font>';
echo '</TD>';
echo '<TD width="58" align="center">';
echo '<font class="tinytextblk" face="Arial" size="1"><strong><a href="/trip_log/trip_log_report.php?trip=',$row['trip_id'],'">',$row['triplocation'],'</a></strong></font>';
echo '</TD>';
echo '<TD width="131" align="center">';
echo '<font class="tinytextblk" face="Arial" size="1">',$row['state'],'</font>';
echo '</TD>';
echo '<TD width="25" align="center">';
echo '<font class="tinytextblk" face="Arial" size="1">';
echo $startdate; if ($enddate!= $startdate) echo ' to ',$enddate;
}
echo '</font>';
echo '</TD>';
echo '<TD width="25" align="center">';
echo '<font class="tinytextblk" face="Arial" size="1">';
if (!empty($row['miles'])) $row['miles'];
else echo '&nbsp;';
echo '</font>';
echo '</TD>';
echo '<TD align="Center" width="52">';
echo '<font class="tinytextblk" face="Arial" size="1">';
echo '<A HREF="javascript:void(0);" onclick="fullScreen();">Slideshow</A>';
echo '</font>';
echo '</TD>';
echo '<TD align="Center" width="37">';
echo '<font class="tinytextblk" face="Arial" size="1">&nbsp;</font>';
echo '</TD>';
echo '<TD align="Center" width="31">';
echo '<font class="tinytextblk" face="Arial" size="1">&nbsp;</font>';
echo '</TD>';
echo '<TD align="Center" width="33">';
echo '<font class="tinytextblk" face="Arial" size="1">&nbsp;</font>';
echo '</TD>';
echo '</TR>';

?>

</TABLE>

outdoorxtreme1

4:16 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



I think there might be a problem with this part of the code:

echo '<font class="tinytextblk" face="Arial" size="1">';
echo $startdate; if ($enddate!= $startdate) echo ' to ',$enddate;
}
echo '</font>';
echo '</TD>';

What does the } mean?

jatar_k

5:09 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that closing brace should match up with an opening brace { farther up in the code

NomikOS

5:11 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



Ok, let's finish this first, and then I will give you a great tip ;)

I see three errors in your code:

1)
echo '<font class="tinytextblk" face="Arial" size="1">',$row['triptype'],'</font>';
you mean this right?
echo '<font class="tinytextblk" face="Arial" size="1">' . $row['triptype'] . '</font>';
points (.) not commas (,)

2)
> > What does the } mean?
this } is part of

while()
statement and is closing output of row before than
echo '</TR>'; 
that's not look good

3) you code say (see comments: //):
echo '<TD width="25" align="center">';
echo '<font class="tinytextblk" face="Arial" size="1">';
if (!empty($row['miles'])) $row['miles']; // <-- missed echo
else echo '&nbsp;';
echo '</font>';
echo '</TD>';

outdoorxtreme1

5:13 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



It doesn't seem to echo anything after } in the html table I am creating untill the last row. Each row before the last one, the cells stop after the date cell. It doesn't show all of the cells that are supposed to be in each row untill the last row on my html table.

outdoorxtreme1

5:16 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



Sorry. I posted that message at the same time you guys were probably writing yours. Let me look at my code and give it a try and I'll get back to you.

jatar_k

5:27 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



NomikOS

echo '<font class="tinytextblk" face="Arial" size="1">',$row['triptype'],'</font>';
you mean this right?
echo '<font class="tinytextblk" face="Arial" size="1">' . $row['triptype'] . '</font>';
points (.) not commas (,)

actually both are correct except the commas are faster. Otherwise the full string must be concatenated before it can be echo'ed

it works nicely for echo

;)

outdoorxtreme1

5:30 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



I finally figured out how to get my table to look right and for everything to output right. I had to move the } at the end of all my echo's

jatar_k

5:31 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nice work :)

outdoorxtreme1

5:32 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



one little minor question.

I can't seem to figure out how to get my date centered in the cell even though the cell centering is there.

echo '<TD width="25" align="center">';
echo '<font class="tinytextblk" face="Arial" size="1">';
echo $startdate; if ($enddate!= $startdate) echo ' to ',$enddate;
echo '</font>';
echo '</TD>';

jatar_k

5:36 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



when you view source I assume the td tag looks right?

maybe the cell is too small?

I am not really sure, I also assume we are talking horizontal align, that html should be right

outdoorxtreme1

5:41 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



is there a way to put <br> between the month and day also between the day and year.

To like this when I view source:
November<br>8<br>2005

Would I edit this line?
$startdate = date("F d Y", mktime(0,0,0,$row['TSpickMonth'],$row['TSpickDay'],$row['TSpickYear']));

jatar_k

5:46 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



because that line constructs the whole thing you would have to replace the spaces with breaks after that line

something like

$startdate = str_replace [php.net](' ','<br>',$startdate);

NomikOS

5:49 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



wonderfull jatar_k! thanks by the magnificent tip about commas.

And now a tip for outdoorxtreme1:

outdoorxtreme1 you will enjoy this:
the following is out of the present discussion, but I think than you may be interested in learn about this type of sintax <<<


# from the PHP manual:
# --------------------
print <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon no extra whitespace!
END;

# in your code would be like this:
# --------------------------------
print <<<END
<TR class="tripSelectRow">
<TD width="65" align="center">
<font class="tinytextblk" face="Arial" size="1">
1) look here: this var is between { and } --> {$row['triptype']}
2) a normal var can be writing as usual $thisVarIsOk_without_{}_signs
</font>
</TD>
...
and so on
...
END

You see the convenience?

1) you don't need "echo" at each line
2) you write less
3) you don't need escape double quotes (")
4) and is much more easy discover errors (because clean code)

if you like it, worry about:
----------------------------

<<<END <-- the word END can be any other: EOM, EOD, EOF, TEXT, CAT, DOG, etc
but must be the same at last:
END or EOM, EOD, EOF, TEXT, CAT, DOG, etc


# example:
----------
echo <<<SAMECAT
<h2>
I have a new $cat. His name is {$cat['name']}.
<br />
Now we are in the "nice cats" park.
</h2>
SAMECAT; <-- without space before SAMECAT; or you will receive an error!

NOTE:
This type of sintax is better only for output various lines.
echo is good most of times.

what do you think?

---
¦O¦

outdoorxtreme1

6:02 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



looks good. I'll have to try it and see how it goes on some code. Thanks for all the help everyone. I seem to have everything solved except the date is not centered. I'll have to look at the cell size.

outdoorxtreme1

7:12 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



How do I sort the Html table rows according to date?

outdoorxtreme1

7:13 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



I have this code but it is not sorting right.

$sql = 'SELECT * FROM trip_log ORDER BY TSpickYear, TSpickMonth, TSpickDay ASC';
$result = mysql_query($sql) or die ('<p>select died: ' . mysql_error());

jatar_k

7:20 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try switching your ASC (ascending) to DESC (descending)

NomikOS

7:20 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



!

outdoorxtreme1

7:24 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



tried that and still the same thing

jatar_k

7:30 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you want newest first then descending should be the way to go

trouble is that the DESC or ASC only applies ot the column preceding it

man would it be a lot easier if you just stored a unix timestamp instead but that can be for your next project ;)

what if you get rid of the ASC or DESC all together and just leave

SELECT * FROM trip_log ORDER BY TSpickYear, TSpickMonth, TSpickDay

that should leave them in an order though it may go the wrong way.

What type are these columns? int? varchar?

outdoorxtreme1

7:33 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



They are text. Is that the problem?

outdoorxtreme1

7:34 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



I want oldest first.

outdoorxtreme1

8:19 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



Should I make dates varchar?

jatar_k

8:20 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you mean the column type is actually TEXT?
This 61 message thread spans 3 pages: 61