Page 1 of 1

anyone feel up to optimizing some code?

Posted: Tue Oct 18, 2005 11:44 am
by Charles256
just curious if anyone can think of a more effecient way to do the following..if not,no big deal:D-

Code: Select all

while ($i=mysql_fetch_object($query))
			{
				if (!isset($a))
				{
					$a=1;
				}
				$sel="SELECT DISTINCT gk_type,kart_name,kart_no FROM kart WHERE gk_type='$i->number' ORDER BY kart_no ASC";
				$query2=mysql_query($sel);
				$count=mysql_num_rows($query2);
				if ($count > 0)
				{
					@$expand=$_REQUEST['expand'.$i->number.''];
					if ($a==1)
					{
						echo ("<b><div align='left'>$i->name");
						if ($expand != "expand".$i->number."")
						{
							echo ("<br /><a href='index.php?page=NULL&expand".$i->number."=expand".$i->number."'>
							<font size='2'>Expand</font></a></div></b><br />");
						}
						else
						{
							echo ("<br /><a href='index.php'>
							<font size='2'>Minimize</font></a></div></b><br />");
						}
					}
					else
					{
						echo ("<br /><br /><b><div align='left'>$i->name");
						if ($expand != "expand".$i->number."")
						{
							echo ("<br /><a href='index.php?page=NULL&expand".$i->number."=expand".$i->number."'>
							<font size='2'>Expand</font></a></div></b><br />");
						}
						else
						{
							echo ("<br /><a href='index.php'>
							<font size='2'>Minimize</font></a></div></b><br />");
						}
					}
					while ($o=mysql_fetch_object($query2))
					{
						@$check="expand".$o->gk_type."";
						if ($expand==$check)
						{
							echo ("&nbsp;<a href='index.php?kart=$o->kart_no'>$o->kart_no $o->kart_name</a><br />");
						}
					}
				}
				$a++;
			}

Posted: Tue Oct 18, 2005 2:39 pm
by Jenk
Try using a join instead of multiple queries (can't tell if this is possible or not as I cannot see the value of $query, nor do I know your tables)

And instead of having $a=1, then a challenge if ($a==1), just echo the containing <div> before you enter the loop and close it after.