Re: Supposed to output three diff items, get 2 of the last item but no others

From: Date: Thu, 08 Nov 2001 14:56:34 +0000
Subject: Re: Supposed to output three diff items, get 2 of the last item but no others
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-73880@lists.php.net to get a copy of this message
Hi Dan, Rule #1 Don't show your passwords (we're all nice guys, but don't tempt us). Rule #2 indent your code so it is more readable. like this if (blah blah) { do this then do another thing and this too } or while (this is happening) { do this and this if (this is true) { do this thing, see how it's indented? } and this } // end while Lastly, and, I think, the heart of your problem. In a FOR loop you have $cat_output = "<center>"; You don't echo $cat_output until after the FOR loop. So, every time that line is encountered, all previous values of $cat_output are wiped out and it will only contain "<center>". You probably want to change that to $cat_output .= "<center>"; hope that is helpful, bill hollett Dan McCullough wrote: > Yes I write long subjects. Yes I'm about to show you some code. :) > Can someone please give me a hint or point out where I am messing up. > I usually code in JSP, which has some similarly, but pulling items from the database should be > giving me 3 categories and two images a piece, but its giving me the last category and giving > it > to me twice. > > Any help would be appreaciated :) > > <? > /* declare connection variables */ > $DBhost = "localhost"; > $DBuser = "root"; > $DBpass = "px18900"; > $DBName = "copeland"; > $id; > /*connect */ > mysql_connect($DBhost,$DBuser,$DBpass) or die ("Unable to connect to database"); > @mysql_select_db("$DBName") or die("Unable to select database $DBName"); > $sql = "SELECT * FROM categories WHERE parent_id = 0 ORDER BY sort_order ASC"; > $result = mysql_query($sql); > if ($result) { > $nrows = mysql_num_rows($result); > } > while ($category = mysql_fetch_array($result)) { > $cat_picture = $category["cat_image"]; > $cat_txt_image = $category["cat_txt_image"]; > $cat_atl_tag = $category["cat_name"]; > //$cat_img_path = "images/product/"; > $cat_image_p = "<img src=\"images/product/".$cat_picture."\" > border=\"0\" > alt=\"".$cat_atl_tag."\">"; > $cat_image_t = "<img src=\"images/product/".$cat_txt_image."\" > border=\"0\" > alt=\"".$cat_atl_tag."\">"; > if ($category[single] == "1") { > /* Go down a level */ > $catid = $category["id"]; > $sqlb = "SELECT collection_id FROM collections WHERE parent_id = $catid"; > $resultb = mysql_query($sqlb); > $collection = mysql_fetch_array($resultb); > $cat_link = "<a > href=test3.php?id=".$collection["collection_id"].">"; > } else { > $cat_link = "<a href=test2.php?id=".$category["id"].">"; > } > for ($i = 0; $i < $nrows; $i++) { > $cat_output = "<center>"; > $cat_output .= > "".$cat_link."".$cat_image_t."".$cat_image_p."<br><br>"; > if ( ($i % 2) == 0) { > $cat_output .= > "".$cat_link."".$cat_image_p."".$cat_image_t."<br><br>"; > } > } > } > $cat_output .= "</center>"; > $cat_breadcrumb = "<font > face=\"Arial,Helvetica,Geneva,Swiss,SunSans-Regular\">products > :</font><br><br>"; > ?> > > <html> > <head> > <title>Test Template</title> > </head> > <body bgcolor="#ffffff" leftmargin="0" topmargin="0" > marginwidth="0" marginheight="0" > rightmargin="0" bottommargin="0"> > <? > print $cat_breadcrumb; > print $cat_output; > ?> > </body> > </html> > > ===== > Dan McCullough > ------------------------------------------------------------------- > "Theres no such thing as a problem unless the servers are on fire!" > h: 603.444.9808 > w: McCullough Family > w: At Work > > __________________________________________________ > Do You Yahoo!? > Find a job, post your resume. > http://careers.yahoo.com

« previous php.general (#73880) next »