jbrill Posted October 11, 2007 Share Posted October 11, 2007 Hey, I'm just working on a little script here.. need some help with it. I have a table that has 12 rows in it. color1, color2, color3, etc... Im making a drop menu with the colors in it, however if the field is not set (or is blank) it wills till appear in my drop menu as a blank spot. im looking for a way to first check to see if there indeed is text in the row, and if there is, then display the title in the menu. Here is an example of my code: echo "<select name=\"color\">"; echo "<option>".$row2['color1']."</option>"; echo "<option>".$row2['color2']."</option>"; echo "<option>".$row2['color3']."</option>"; echo "<option>".$row2['color4']."</option>"; echo "<option>".$row2['color5']."</option>"; echo "<option>".$row2['color6']."</option>"; echo "<option>".$row2['color7']."</option>"; echo "<option>".$row2['color8']."</option>"; echo "<option>".$row2['color9']."</option>"; echo "<option>".$row2['color10']."</option>"; echo "<option>".$row2['color11']."</option>"; echo "<option>".$row2['color12']."</option>"; echo "</select>"; Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted October 11, 2007 Share Posted October 11, 2007 <?php if (empty($row['color1'])) { continue;} else { echo "Error: no color set for color 1"; }?> Quote Link to comment Share on other sites More sharing options...
trq Posted October 11, 2007 Share Posted October 11, 2007 Firstly, having the fields color1-12 in your database is a pretty poor design. But, yeah, one solution would be to use an if, or even the ternary operator. eg; <?php echo "<select name=\"color\">"; echo ($row2['color1'] != '') ? "<option>{$row2['color1']}</option>" : ""; echo ($row2['color2'] != '') ? "<option>{$row2['color2']}</option>" : ""; echo ($row2['color3'] != '') ? "<option>{$row2['color3']}</option>" : ""; echo ($row2['color4'] != '') ? "<option>{$row2['color4']}</option>" : ""; echo ($row2['color5'] != '') ? "<option>{$row2['color5']}</option>" : ""; echo ($row2['color6'] != '') ? "<option>{$row2['color6']}</option>" : ""; echo ($row2['color7'] != '') ? "<option>{$row2['color7']}</option>" : ""; echo ($row2['color8'] != '') ? "<option>{$row2['color8']}</option>" : ""; echo ($row2['color9'] != '') ? "<option>{$row2['color9']}</option>" : ""; echo ($row2['color10'] != '') ? "<option>{$row2['color10']}</option>" : ""; echo ($row2['color11'] != '') ? "<option>{$row2['color11']}</option>" : ""; echo ($row2['color12'] != '') ? "<option>{$row2['color12']}</option>" : ""; echo "</select>"; ?> There is a link in my signiture to a book. In that book there is a section on databases and in particular database normalization techniques. This would help you with a better db design. Quote Link to comment Share on other sites More sharing options...
littledragon Posted October 11, 2007 Share Posted October 11, 2007 That row2 variable is an array, so use a loop like foreach(){ <?php foreach($row2 as $color){ if($color !== ''){ echo "<option>".$color."</option>"; } } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted October 11, 2007 Share Posted October 11, 2007 littledragon, Correct about it being an array but you don't know if it is the whole array. There could also be $row2['id'], $row2['widget'], $row2['anything_else'] that shouldn't appear in the options. Without seeing the original query or table structure you don't know. Quote Link to comment Share on other sites More sharing options...
littledragon Posted October 11, 2007 Share Posted October 11, 2007 littledragon, Correct about it being an array but you don't know if it is the whole array. There could also be $row2['id'], $row2['widget'], $row2['anything_else'] that shouldn't appear in the options. Without seeing the original query or table structure you don't know. true, I'm forgetting it's a mysql query, my bad Quote Link to comment Share on other sites More sharing options...
jbrill Posted October 11, 2007 Author Share Posted October 11, 2007 yes, you are correct there are other $row2['otherthings'] as far as the database structure, it does pull colors form a separate table but each product has a total of 12 different colors. like i said only a color id form another table is put into the "color#" fields Quote Link to comment Share on other sites More sharing options...
jbrill Posted October 11, 2007 Author Share Posted October 11, 2007 using this technique: echo ($row2['color1'] != '') ? "<option>{$row2['color1']}</option>" : ""; im getting the following error: Parse error: syntax error, unexpected T_STRING in /../../details.php on line 140 can anyone spot the error? Quote Link to comment Share on other sites More sharing options...
marcus Posted October 11, 2007 Share Posted October 11, 2007 for($i=1;$i<=12;$i++){ if(strlen($row['color' . $i]) > 0){ echo "<option>{$row['color'.$i]}</option>\n"; } } Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 11, 2007 Share Posted October 11, 2007 <?php echo "<select name=\"color\">"; for ($count =1; $count<=$yourvalue; $count++){ $colors ='color'.$count; echo ($row2[$colors] != '') ? "<option>".$row2[$colors]."</option>" : ""; } echo "</select>"; ?> Quote Link to comment Share on other sites More sharing options...
jbrill Posted October 11, 2007 Author Share Posted October 11, 2007 <?php echo "<select name=\"color\">"; for ($count =1; $count<=$yourvalue; $count++){ $colors ='color'.$count; echo ($row2[$colors] != '') ? "<option>".$row2[$colors]."</option>" : ""; } echo "</select>"; ?> this didnt work either got this error: Parse error: syntax error, unexpected ';' in /.../.../.../.../details.php on line 141 line 141 is: for ($count =1; $count<=$yourvalue; $count++){ Quote Link to comment Share on other sites More sharing options...
littledragon Posted October 11, 2007 Share Posted October 11, 2007 <?php echo "<select name=\"color\">"; for ($count =1; $count<=$yourvalue; $count++){ $colors ='color'.$count; echo ($row2[$colors] != '') ? "<option>".$row2[$colors]."</option>" : ""; } echo "</select>"; ?> this didnt work either got this error: Parse error: syntax error, unexpected ';' in /.../.../.../.../details.php on line 141 line 141 is: for ($count =1; $count<=$yourvalue; $count++){ use mgallforever's reply, definitely should work Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 11, 2007 Share Posted October 11, 2007 i guess the error is not there! yah try megal flowers code but if theres still an error im sure its your code before those code we gave you Quote Link to comment Share on other sites More sharing options...
jbrill Posted October 11, 2007 Author Share Posted October 11, 2007 i guess the error is not there! yah try megal flowers code but if theres still an error im sure its your code before those code we gave you still no luck using that code... thhis is how i have it : echo "<select name=\"color\">"; for($i=1;$i<=12;$i++){ if(strlen($row2['color' . $i]) > 0){ echo "<option>{$row2['color'.$i]}</option>\n"; } } echo "</select>"; gives me this error: Parse error: syntax error, unexpected T_IF in /.../.../.../.../details.php on line 141 line 141 is: if(strlen($row2['color' . $i]) > 0){ and its definatly int he code, if i take that menu im making, the page works fine Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 11, 2007 Share Posted October 11, 2007 just to debug run your page without those codes we gave you if theres still an error post atleast 15 lines before those codes we gave you Quote Link to comment Share on other sites More sharing options...
jbrill Posted October 11, 2007 Author Share Posted October 11, 2007 <?php // Usually you would get your products from a database but we'll pretend.. $products = array(); $products[1] = array("id"=>$row2['id'],"name"=>$row2['name'],"price"=>$row2['price']); // check to see if any items are being added if($_POST['add']) { $product = $products[$_POST['id']]; $cart->add_item($row2['id'],$_POST['qty'],$row2['price'],$row2['name']); } if($_POST['remove']) { $rid = intval($_POST['id']); $cart->del_item($rid); } // spit some forms // You can have many different types of forms, such as many quantity boxes // and an "add to cart" button at the bottom which adds all items // but for the purposes of this demo we will handle one item at a time. echo "<table>"; echo "<tr><td><form method='get' action='cart.php'>"; echo "<input type='hidden' name='id' value='".$row2['id']."'/> "; echo $row2['name']; echo "<select name=\"color\">"; for($i=1;$i<=12;$i++){ if(strlen($row2['color' . $i]) > 0){ echo "<option>{$row2['color'.$i]}</option>\n"; } } echo "</select>"; echo ' $'.number_format($row2['price'],2)." "; echo "<input type='text' name='qty' size='5' value='1'><input type='submit' value='Add to cart' name='add'>"; echo "</form></td></tr>"; } echo "</table>"; ?> Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 12, 2007 Share Posted October 12, 2007 all codes given to you are working. the last code you post run without error /.../.../.../.../details.php can we see this page Quote Link to comment Share on other sites More sharing options...
jbrill Posted October 12, 2007 Author Share Posted October 12, 2007 would you liek to see the code, or the actual site? Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 12, 2007 Share Posted October 12, 2007 actual site maybe first! ??? Quote Link to comment Share on other sites More sharing options...
littledragon Posted October 12, 2007 Share Posted October 12, 2007 actual site maybe first! ??? Now, now... if there's an unexpected element then there's possibly an if statement or for loop or something before the code that never got closed. have a look, and paste the whole page here if you don't see it Quote Link to comment Share on other sites More sharing options...
jbrill Posted October 12, 2007 Author Share Posted October 12, 2007 i don' see any issues...here is the whole page's code. thanks again guys <?php $page_id ='1'; include 'includes/dbconnect.php'; include 'includes/header.php'; include 'includes/leftbar.php'; ?> <div id="maincontent"> <?php include 'includes/subcat_header.php'; $curcat = $_GET['cat']; $cursubcat = $_GET['subcat']; $cat = (isset($curcat)) ? $curcat : ''; $sSubCatAndClause = ''; if($cursubcat != ''){ $sSubCatAndClause = " AND subcat = '" . $cursubcat . "' "; } $sSubCatAndClause2 = ''; if($cursubcat != ''){ $sSubCatAndClause2 = "&subcat=" . $cursubcat . ""; } ?> <?php $query = "SELECT * FROM products WHERE id=".$_GET['id']." AND publish = 1"; $rs = mysql_query($query); while($row2 = mysql_fetch_array($rs)) { echo "<div style=\"text-align: center; padding: 40px;\">"; if($row2['photo']==null || !file_exists($row2['photo'])) { echo "<img src=\"images/nophoto.gif\" width=\"200\" height=\"200\" border=0>"; } else { // if image exists, resize and display $size = getimagesize($row2['photo']); $width = $size[0]; $height = $size[1]; $max = 200; if($width > $max || $height > $max) { $ratio = $width/$height; if($width < $height) { // width is smaller than height, hence the height should be adjuste to max first. $resize = $max/$height; $nheight = 180; $nwidth = ceil($width*$resize); } if($width > $height) { // height is smaller than width, hence the width should be adjuste to max first. $resize = $max/$width; $nwidth = 200; $nheight = ceil($height*$resize); } } else { $nwidth=$width; $nheight=$height; } echo "<a href=\"/haney/". $row2['photo'] ."\" target=\"_blank\"><img class=\"img\" src=\"".$row2['photo']."\" border=0 width=\"".$nwidth."\" height=\"".$nheight."\"></a>"; } echo "</div>"; echo "<div style=\"float: left; width: 100%;\">"; echo "<strong>Product Name:</strong> ". $row2['name'] ."<br>"; echo "<strong>Product Number:</strong> ". $row2['prod_number'] ."<br>"; echo "<strong>Color(s):</strong> ". $row2['color'] ."<br>"; echo "<strong>Size(s):</strong> ". $row2['size'] ."<br>"; echo "<strong>Price:</strong> ". $row2['price'] ."<br>"; echo "<strong>Priced Per:</strong> ". $row2['quantity'] ."<br>"; echo "</div>"; echo "<div style=\"float: left; margin-top: 10px; width: 100%;\">"; echo "<strong>Description:</strong> <br>". $row2['description'] ."<br>"; echo "</div>"; ?> <div id="addtocart"> <?php // Usually you would get your products from a database but we'll pretend.. $products = array(); $products[1] = array("id"=>$row2['id'],"name"=>$row2['name'],"price"=>$row2['price']); // check to see if any items are being added if($_POST['add']) { $product = $products[$_POST['id']]; $cart->add_item($row2['id'],$_POST['qty'],$row2['price'],$row2['name']); } if($_POST['remove']) { $rid = intval($_POST['id']); $cart->del_item($rid); } // spit some forms // You can have many different types of forms, such as many quantity boxes // and an "add to cart" button at the bottom which adds all items // but for the purposes of this demo we will handle one item at a time. echo "<table>"; echo "<tr><td><form method='get' action='cart.php'>"; echo "<input type='hidden' name='id' value='".$row2['id']."'/> "; echo $row2['name']; echo "<select name=\"color\">"; for($i=1;$i<=12;$i++){ if(strlen($row2['color' . $i]) > 0){ echo "<option>{$row2['color'.$i]}</option>\n"; } } echo "</select>"; echo ' $'.number_format($row2['price'],2)." "; echo "<input type='text' name='qty' size='5' value='1'><input type='submit' value='Add to cart' name='add'>"; echo "</form></td></tr>"; } echo "</table>"; ?> </div> </div> <?php include 'includes/footer.php'; ?> Quote Link to comment Share on other sites More sharing options...
littledragon Posted October 12, 2007 Share Posted October 12, 2007 Are you absolutely sure it's an unexpected T_IF on line 141??? Quote Link to comment Share on other sites More sharing options...
trq Posted October 12, 2007 Share Posted October 12, 2007 <?php echo "<select name=\"color\">"; for($i = 1;$i <= 12; $i++) { echo (($row2[${'color1'}.$i] != '') ? "<option>" . $row2[${'color'}.$i] . "</option>" : ""); } echo "</select>"; ?> That should work fine. [quote]as far as the database structure, it does pull colors form a separate table but each product has a total of 12 different colors[/quote] You still should look at database normalization techniques. The way you have things setup, your products can only ever come in a maximum of 12 colors unless you change the database structure. Normalization [i]is[/i] is the key to dynamic data. Quote Link to comment Share on other sites More sharing options...
jbrill Posted October 12, 2007 Author Share Posted October 12, 2007 Hey guys, still no luck here. I can't seem to find the problem using this code: echo "<select name=\"color\">"; for($i = 1;$i <= 12; $i++) { echo (($row2[${'color1'}.$i] != '') ? "<option>" . $row2[${'color'}.$i] . "</option>" : ""); } echo "</select>" getting this error: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/bbold07/public_html/haney/details.php on line 134 on this line: echo "<select name=\"color\">"; Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted October 12, 2007 Share Posted October 12, 2007 echo "<select name=color>"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.