[thelist] php function...

John Corry webshot at members.evolt.org
Thu Apr 11 04:30:00 CDT 2002


its late and my code isnt' working...

I have this query, 'select itemID, itemAvail, itemName, catID, catName from
items, cat where items.itemCat = cat.catID and ((items.itemAvail = $m) ||
(items.itemAvail = 3))'...when I substitute $m for a value (1 or 2) and run
this in PHPMyAdmin, I get rows back.

When I run it in a function, no rows are returned. The query doesn't break,
it just returns 0 rows.

Why is that? Anything I can do to fix it? The function accepts the value of
$m, I know it isn't that...it just doesn't successfully get rows from the
db, even though my query function safe_query() runs without breaking...

I don't get it...I've been staring at it for a few hours and can't see
anything wrong.

John

<code below>
<?php
function build_nav($cat, $m)
{
// default class for shipping/delivery links is 'nav' these are variables
that will change depending on the value of $m;
$ship_head = "<a href=\"category.php?m=1\">Send flowers to the
mainland</a>";
$del_head = "<a href=\"category.php?m=2\">Send flowers on Maui</a>";
$j = '';
$query = "select itemID, itemAvail, itemName, catID, catName from items, cat
where items.itemCat = cat.catID and ((items.itemAvail = $m) ||
(items.itemAvail = 3))";

//if $m == 1
if ($m == 1)
	{
	// run the query
	$result = safe_query($query);
	while ($row == mysql_fetch_array($result))
		{
		$cat_id = $row['catID'];
		$cat_name = $row['catName'];
		$item_id = $row['itemID'];
		$item_name = $row['itemName'];

		if ($cat_id != $j)
			{
			$arr_cat_nav[] .= "<div class=\"catnav\"><a
href=\"category.php?cat=$cat_id&m=$m\">$cat_name</a></div>";
			}
		$j = $cat_id;

		$arr_item_nav[] = "<div class=\"itemnav\"><a
href=\"item.php?item=$item_id\">$item_name</a>";
		}


	//	loop through $arr_cat_nav
	for ($i=0; $i < count($arr_cat_nav); $i++)
	//		append $ship_cat_nav with link <div> from $arr_cat_nav[];
		{
		$ship_cat_nav .= $arr_cat_nav[$i]."\n";
		}
	// set the head
	$ship_head = "<div class=\"on\">Send flowers to the mainland</div>";
	// end if
	}
unset($i);
unset($j);
unset($row);
//unset($result);

//if $m == 2
if ($m == 2)
	{
	//	$ship_on = "on";
	$del_on = "on";
	// run the query
	$result = safe_query($query);
	while ($row == mysql_fetch_array($result))
		{
		$cat_id = $row['catID'];
		$cat_name = $row['catName'];
		$item_id = $row['itemID'];
		$item_name = $row['itemName'];

		if ($j != $cat_id)
			{
			$arr_cat_nav[] = "<div class=\"catnav\"><a
href=\"category.php?cat=$cat_id&m=$m\">$cat_name</a></div>";
			}
		$j = $cat_id;

		$arr_item_nav[] = "<div class=\"itemnav\"><a
href=\"item.php?item=$item_id\">$item_name</a>";
		}


	//	loop through $arr_cat_nav
	for ($i=0; $i < count($arr_cat_nav); $i++)
	//		append $ship_cat_nav with link <div> from $arr_cat_nav[];
		{
		$del_cat_nav .= $arr_cat_nav[$i]."\n";
		}
	// set the nav
	$del_head = "<div class=\"on\">Send flowers on Maui</div>";

	// end if
	}
unset($i);
unset($j);
unset($row);
//unset($result);

//print each element


$arr_global_nav[0] = "<div class=\"nav\"><a
href=\"index.php\">Home</a></div>";
$arr_global_nav[1] = "<div class=\"nav\">". $ship_head . $ship_cat_nav .
"</div>";
$arr_global_nav[2] = "<div class=\"nav\">". $del_head . $del_cat_nav .
"</div>";
$arr_global_nav[3] = "<div class=\"nav\"><a href=\"wedding.php\">Wedding
Flowers</a></div>";
$arr_global_nav[4] = "<div class=\"nav\"><a href=\"about.php\">About Maui
Blooms</a></div>";
$arr_global_nav[5] = "<div class=\"nav\"><a
href=\"faq.php\">FAQs</a></div>";
$arr_global_nav[6] = "<div class=\"nav\"><a href=\"contact.php\">Contact
Us</a></div>";

// if the function is called with no arguments
//don't query the database, just make $navbar the global nav:

// loop through array
for ($i=0; $i < count($arr_global_nav); $i++)
	{
	$navbar .=  $arr_global_nav[$i]."\n";
	// end for
	}
// end if

// diagnostic output
$navbar .= "\$array_cat_nav =".count($array_cat_nav);
$navbar .= "<br>";
$navbar .= "query returned".count($result)."rows";

// return the finished navigation bar
return $navbar;
// end function
}
?>
</code below>




More information about the thelist mailing list