Jump to content

wat should i use in this situation...? if statement?


jbrill

Recommended Posts

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>";

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

<?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++){

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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>";

?>

Link to comment
Share on other sites

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'; ?>

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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\">";

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.