Page 1 of 1

Need help with PHP code

Posted: Sun Oct 26, 2003 3:00 pm
by charp
This is my first post in this forum. Let's help the newbie out, OK?

I'm trying to modify the following code to output results only if one particular field is not empty. If the field "complete" has any text at all (is not empty) then I would like the current code to proceed as normal. However, if the "complete" field is empty, I'd like to skip to the next entry in the database in which the "complete" field is again not empty.

Can anyone help out? I'm not trained in PHP at all, but I tinker with confidence.

Code: Select all

<?php
echo "<p align="left">";
$configfile = "/home/uplandhi/php_configs/news_config.php";
require $configfile;

if (!$pageNum)
&#123;
$pageNum=0;
&#125;

$fullresult = mysql_query("SELECT * FROM ".$table." ORDER BY date DESC",$db);

$numRows = mysql_num_rows($fullresult);

$highNum = ($pageNum+1)*$entriesToPage;

$lowNum = ($pageNum*$entriesToPage);

$numOfPages = $numRows/$entriesToPage;
$numOfPages = ceil($numOfPages);
$actualPageNum = $pageNum+1;

echo "<table width="100%" border="0" cellspacing="0" cellpadding="4">";

$smallresult = mysql_query("SELECT * FROM `$table` ORDER BY `id` DESC LIMIT $lowNum, $entriesToPage",$db);

if ($therow = mysql_fetch_array($smallresult))
&#123;
&#123;
do
&#123;
echo "<tr valign="top">";
echo "<td width="35" background="" class="anndate">";
printf($therow&#1111;"date"]);
echo "</td>";
echo "<td background="" class="small"><b>";
printf($therow&#1111;"title"]);
echo ":&nbsp;</b>";
printf($therow&#1111;"full"]);
echo "</td></tr>";
echo "<tr valign="top">";
echo "<td colspan="2" background="" class="small">";
printf($therow&#1111;"complete"]);
echo "</td></tr>";

&#125;
while ($therow = mysql_fetch_array($smallresult));
&#125;

echo "<tr valign="top">";
echo "<td width="35" background="" class="anndate">&nbsp;</td>";
echo "<td background="" class="small">";
echo "Showing page $actualPageNum of $numOfPages";
echo "</td></tr>";

echo "<tr valign="top">";
echo "<td width="35" background="" class="anndate">&nbsp;</td>";
echo "<td background="" class="small">";

if($numOfPages>1)
&#123;
if($pageNum!=0)
&#123;
$previousPage = $pageNum-1;
echo "<< <a href="index.php?pageNum=$previousPage">Previous</a>&nbsp;";
&#125;
$countPages=1;
do
&#123;
$goToPage=$countPages-1;
if($countPages==$actualPageNum)
&#123;
echo "&nbsp;<b>$countPages</b>&nbsp;";
&#125;
else
&#123;
echo "&nbsp;<a href="index.php?pageNum=$goToPage">$countPages</a>&nbsp;";
&#125;
$countPages++;
&#125;
while($countPages<=$numOfPages);

if($numOfPages-1!=$pageNum)
&#123;
$nextPage = $pageNum+1;
echo "&nbsp;<a href="index.php?pageNum=$nextPage">Next</a> >>";
&#125;
&#125;
&#125;

else
&#123;
echo "There were no records";
&#125;

echo "</td></tr>";
echo "</table>";
?>

Posted: Sun Oct 26, 2003 4:01 pm
by volka
is the field empty (i.e. == NULL) or is it a string with length 0?
If it really is NULL you might change your query from
"SELECT * FROM ".$table." ORDER BY date DESC"
to

Code: Select all

"SELECT * FROM ".$table." WHERE complete IS NOT NULL ORDER BY date DESC"
If it is a string of legth 0 you might use

Code: Select all

"SELECT * FROM ".$table." WHERE length(complete) > 0 ORDER BY date DESC"
(which you might also use if it is a NULL-value, but it's slower)

Posted: Sun Oct 26, 2003 10:20 pm
by charp
OK, this is great. Not only did I get an answer, but I practically understand the entire answer. Without even trying either solution, I'm sure one or the other will work. However, I would like to completely understand the difference between an empty field (i.e. == NULL) and a string with length 0. My neophyte instinct says they're the same, but obviously not or the great volka would not have made the distiction.

Volka, or anyone else, can you bear with my ignorance and help out with an explanation? The "complete" field is for text, but will sometimes be left blank. Does that make it NULL?

Thanks. You guys (and gals) that offer your expertise are awesome!

UPDATE

As I said, one of the suggested solutions worked. For the record, it was the second one using length(complete) > 0. Yippee!

Posted: Mon Oct 27, 2003 7:56 am
by volka
the mysql people exactly knew that there will be questions like that ;)
http://www.mysql.com/doc/en/Problems_with_NULL.html

Posted: Mon Oct 27, 2003 7:38 pm
by charp
Many thanks to volka! I'm just discovering the power of PHP, so I know next to nothing. Gotta get some books to teach myself about PHP and MySQL.

Posted: Mon Oct 27, 2003 7:53 pm
by JAM
charp wrote:OK, this is great. Not only did I get an answer, but I practically understand the entire answer.

...

As I said, one of the suggested solutions worked. For the record, it was the second one using length(complete) > 0. Yippee!
That's nice to hear. To often there is the feeling of answers or ideas given that simply solves a problem without learning.

For others; please note the good use of a "for the record, this worked: ..." solution given. I'm taking any chance I get to address this, as to many skips this part, and by that gives those using the search ability of the forum a hard time finding help.