Jump to content

Where would i put a conditional substr?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Cyberen

Cyberen

    Newbie

  • Members
  • PipPip
  • 19 posts
So I've got this chunk of code which does three things:

1. Extracts values from a database

2. Displays them as a color options menu

3. When an option is selected, the picture changes and the selection on the drop-down menu on the bottom of the screen changes.

For instance, clicking a Black and White image icon changes the big glass picture to a Black and White shade, and the color selected to be ordered is Black and White.

What I want to do is make an exception for every color option that starts with the string "snapshot". Upon detecting this string, the color is not shown in the palette but in a second list of thumbnails. The second list can show larger images upon clicking just like the first, but will not be an option on the drop-down menu on the bottom. Where do I put the conditional statements to make this happen?

Here is the PHP code that I have so far. The two tables are labeled. I trimmed the code down for your convenience so it may not work if you tested the code as-is.
The normal color pallete code starts around line 121 and the alternate view code (basically a copy) starts around 166.

<?

ini_set('display_errors',1);

ini_set('error_reporting', E_ALL);

ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

error_reporting(E_ALL | E_STRICT);

ini_set('display_errors','Off'); 

include 'db.php';

if (!isset($_POST['action']))

{

//If not isset -> set with dumy value

$_POST['action'] = "undefine";

} 

session_start();

header("Cache-control: private");

//add to cart

if($_POST['action']=='add'){

  if(!isset($_SESSION['cart'])){

    $_SESSION['cart']=array(); 

  }

  $item=$_POST['id'] . '||';

  $item.=$_POST['glass'] . '||'; 

  $item.=$_POST['finishes'] . '||'; 

  $item.=$_POST['stocks'] . '||';

  $item.=$_POST['cord_colors'] . '||';

  $item.=$_POST['ceiling'] . '||';

  $item.=$_POST['glass_shape'] . '||';

  $item.=$_POST['leafs'] . '||';

  $item.=$_POST['ceil_cap'];

  array_push($_SESSION['cart'],$item);

  header("Location: viewcart2.php?back=" . urlencode("pro.php?id=" . $_POST['id']));

  exit;

}

?>

</td></tr></table>

      <!-- end #header --></div>

<?

/* Display categories */

$query = "SELECT id,category,description from categories where id!=11 and deleted=0 order by category";

$result = mysql_query($query) or die("Query failed : " . mysql_error());

$last=0;

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

  if($last>0){

    ?>

    <!-- | -->

    <?

    }

    ?>

    <!--<a href="cat.php?id=<?= $line['id'] ?>" class="sub"><?= $line['category']

    ?></a>-->


    <?

    if($_GET['id']==$line['id'] || $last==0){

      $description=stripslashes($line['description']);

      $title=stripslashes($line['category']);

      $last=$line['id'];

    }

  }

  if(isset($_GET['id'])){

 $last=$_GET['id'];

}


mysql_free_result($result);




//get product details and convert them into local variables

$query = "SELECT * from products where deleted=0 and id=" . $_GET['id'];

$result = mysql_query($query) or die("Query failed : " . mysql_error());

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

  $name = stripslashes($line['name']);

  $description = stripslashes($line['description']);

  $retail = stripslashes($line['retail']);

  $price = stripslashes($line['price']);

  $bulb = stripslashes($line['bulb']);

  $redirect = stripslashes($line['redirect']);

  $ceiling = stripslashes($line['ceiling']);

  $dims = stripslashes($line['dims']);

  $static = stripslashes($line['static']);

}

mysql_free_result($result);

//determining if product has signature or radiance glass, also sets values for cap n pole image

$colorsetname = "Signature";

if ((isset($name))&&((substr($name,14,8)=="Radiance")||(substr($name,0,8)=="Radiance")))

	{$viewcol=5;

	$colorsetname = "Radiance";}

else {$viewcol=6;}

$ispendant = 16;

$redirviewcol = ($viewcol+999);


?>

              <table height="530" width="890" border="0" cellpadding="4" cellspacing="0">

              <tr>

                <td width="560" height="371" align="middle" valign="middle"> 

                  <img name="mainimg" id="mainimg" src="<? 


//get main image

$query = "SELECT * from glass where deleted=0 and pid=" . $_GET['id'] . " order by color limit 1";

$result = mysql_query($query) or die("Query failed : " . mysql_error());

 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

   echo $line['url'];

 }

mysql_free_result($result);

?>" >


<!-- width="263" height="353" -->

</td><? if(($_GET['id']==1004)||($_GET['id']==1005))

{echo '</tr><tr>';}?>

   <td valign="middle" align ="center"> 

    <table border="0" cellpadding="0" cellspacing="0">

       <tr>

         <td valign="top" align="center">


		 <? if($_GET['id']!=14){ ?>

		 <table width="239" border="0" cellspacing="0" cellpadding="0">

         

<?

$narrows=3;

if(($_GET['id']==1004)||($_GET['id']==1005))

{$narrows=5;}

//This is the normal color pallete menu

$query = "SELECT * from glass where deleted=0 and pid=" . $_GET['id'] . " order by color";

$result = mysql_query($query) or die("Query failed : " . mysql_error());

if(mysql_num_rows($result)>1){

  echo '<tr valign="top"> 

  <td colspan="4"><span class="sub">Colors</span> 

  <span class="small">(click swatch to see item in that color glass)</span>';

if ($narrows==3)

{echo '<br />Click to order a <a href="pro.php?id='.$redirviewcol.'">'.$colorsetname.'</a> glass sample!</a></td></tr>';}

  else {echo'</tr>';}

  echo '<tr>';

  $cnt=0;

  while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

    echo '<td><img src="' . $line['color_url'] . '" width="120" height="61" hspace="4" vspace="4" 

    border="0" onclick="change_glass(' . "'" . $line['color'] . "'" . ');document.mainimg.src=' . "'" . $line['url'] . "'" . '">

    </td>';

    $cnt++;

    $lookupcolor.=" if(col=='" . $line['color'] .  "'){ return '" . $line['url'] . "'; }\n";

	if($cnt % $narrows == 0){

      echo '</tr><tr>';

    }

  }

  echo '</tr>';

}

mysql_free_result($result);

$query = "SELECT * from finishes where deleted=0 and pid=" . $_GET['id'] . " order by finish";

$result = mysql_query($query) or die("Query failed : " . mysql_error());

?>

</table></td></tr><tr><td align="center">

<?PHP

 } //end id!=14 ?>


	</td>

  </tr>

</table>

</td>

</tr>

<tr>

<td valign="top" align="center">


		 <? if($_GET['id']!=14){ ?>

		 <table width="239" border="0" cellspacing="0" cellpadding="0">

         

<?

$narrows=5;

//This is for the alternate or installed views of the product in a separate table

$query = "SELECT * from glass where deleted=0 and pid=" . $_GET['id'] . " order by color";

$result = mysql_query($query) or die("Query failed : " . mysql_error());

if(mysql_num_rows($result)>1){

  echo '<tr valign="top"> 

  <td colspan="4"><span class="sub">Installed or Alternate Views</span>';}

  else {echo'</tr>';}

  echo '<tr>';

  $cnt=0;

  while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

    echo '<td><img src="' . $line['color_url'] . '" width="120" height="61" hspace="4" vspace="4" 

    border="0" onclick="change_glass(' . "'" . $line['color'] . "'" . ');document.mainimg.src=' . "'" . $line['url'] . "'" . '">

    </td>';

    $cnt++;

    $lookupcolor.=" if(col=='" . $line['color'] .  "'){ return '" . $line['url'] . "'; }\n";

	if($cnt % $narrows == 0){

      echo '</tr><tr>';

}

  }

  echo '</tr>';

}

mysql_free_result($result);

$query = "SELECT * from finishes where deleted=0 and pid=" . $_GET['id'] . " order by finish";

$result = mysql_query($query) or die("Query failed : " . mysql_error());

?>

</table></td></tr><tr><td align="center">

	</td>

</tr>

</tr>

<tr>

  <td height="77" colspan="2" valign="middle" border="0">

  <br /><a href="javascript:;"> </a></span> 


<b>


<!-- Product description -->

<?= $name ?></b><br><br>

<?= $description; ?>

<br />

<br />

<?= $redirect; ?>

<br />

<br />

<?= 

$nostr = "";

  $showdims = substr($dims, 0, 70);

  echo $showdims;

?>                     

<br />

<br />

<?= $bulb; ?>

<div id="notify_price"></div>

</td></td>

    <tr>

      <td align="center" colspan="2"><form name="form1" method="post" action="pro.php">

     	  <input type="hidden" name="action" value="add">

		  <input type="hidden" name="id" value="<?= $_GET['id']; ?>">

          <table border="0" cellspacing="10" cellpadding="0">

             <tr>

<?

$header_number=1;


//get glass color if id not equal to 14

if($_GET['id']!=14){ 

  $query = "SELECT * from glass where deleted=0 and pid=" . $_GET['id'] . " order by color";

  $result = mysql_query($query) or die("Query failed : " . mysql_error());

   if(mysql_num_rows($result)>1){

    echo '<td valign=top style="height: 75"><a href="javascript:void(viewer'.$viewcol.'.show(0))" style="text-decoration:none"><img src="images/Circle' . $header_number . '.png" class="nob">   <img src="images/GlassColor.png"></a><br /><select name="glass" onChange="document.mainimg.src=lookupcolor(document.form1.glass.value)">';

    $header_number++;

    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

      echo '<option value="' . $line['color']  . '">' . $line['color'] .  '</option>';

    }

    echo '</select></td>';

  }


  mysql_free_result($result);

}//end id!=14

?>

Thanks for reading!

#2
Cyberen

Cyberen

    Newbie

  • Members
  • PipPip
  • 19 posts
After a night of sittin' down at a Denny's and actually having time to go through this without being in panic mode I got a code to work. Huzzah and a big thanks to Calculon000!

here's the PHP conditional statement that was figured out:

//This is for the alternate or installed views of the product in a separate table

$query = "SELECT * from glass where deleted=0 and pid=" . $_GET['id'] . " order by color";

$result = mysql_query($query) or die("Query failed : " . mysql_error());


   while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { 

if(substr($line['color'],0,8)=="snapshot")

{


if((mysql_num_rows($result)>1)){

  echo '<tr valign="top"> 

  <td colspan="4"><span class="sub">Installed or Alternate Views</span>';}

  else {echo'</tr>';}

  echo '<tr>';

  $cnt=0;

 

 //next three lines check for snapshot in the string


    echo '<td><img src="' . $line['color_url'] . '" width="120" height="61" hspace="4" vspace="4" 

    border="0" onclick="change_glass(' . "'" . $line['color'] . "'" . ');document.mainimg.src=' . "'" . $line['url'] . "'" . '">

    </td>';

    $cnt++;

    $lookupcolor.=" if(col=='" . $line['color'] .  "'){ return '" . $line['url'] . "'; }\n";

	if($cnt % $narrows == 0){

      echo '</tr><tr>';

    }

  

  //next twelve lines check for snapshot in the string and do something

}

}

}

mysql_free_result($result);

$query = "SELECT * from finishes where deleted=0 and pid=" . $_GET['id'] . " order by finish";

$result = mysql_query($query) or die("Query failed : " . mysql_error());

as you can see the conditional string came after the MYSQL_ASSOC line. The code above is for alternate views. Here's the code to exclude alternate views from the main pallette.

//This is for the alternate or installed views of the product in a separate table

$query = "SELECT * from glass where deleted=0 and pid=" . $_GET['id'] . " order by color";

$result = mysql_query($query) or die("Query failed : " . mysql_error());

if(mysql_num_rows($result)>1){

  echo '<tr valign="top">

  <td colspan="4"><span class="sub">Colors</span> 

  <span class="small">(click swatch to see item in that color glass)</span>';

if ($narrows==3)

{echo '<br />Click to order a <a href="pro.php?id='.$redirviewcol.'">'.$colorsetname.'</a> glass sample!</a></td></tr>';}

  else {echo'</tr>';}

  echo '<tr>';

  $cnt=0;

 

 //next three lines check for snapshot in the string

   while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { 

if(substr($line['color'],0,8)!="snapshot")

{ 

  {

    echo '<td><img src="' . $line['color_url'] . '" width="120" height="61" hspace="4" vspace="4" 

    border="0" onclick="change_glass(' . "'" . $line['color'] . "'" . ');document.mainimg.src=' . "'" . $line['url'] . "'" . '">

    </td>';

    $cnt++;

    $lookupcolor.=" if(col=='" . $line['color'] .  "'){ return '" . $line['url'] . "'; }\n";

	if($cnt % $narrows == 0){

      echo '</tr><tr>';

    }

  

  //next twelve lines check for snapshot in the string and do something

}

  }

}

}

mysql_free_result($result);

$query = "SELECT * from finishes where deleted=0 and pid=" . $_GET['id'] . " order by finish";

$result = mysql_query($query) or die("Query failed : " . mysql_error());

There is just one minor issue left which shouldn't take too much to solve.

When the person selects the snapshot image, it shows up as an option in the drop-down list at the bottom of the screen. How do I make a conditional statement that removes it from being an option? I tried simply making a conditional statement where if the $line[color] had the string that identifies it as a snapshot, the color option html doesn't even occur. But for some reason this prevents the ability to click the thumbnail to see the bigger picture.
I have attached the PHP of the file with the particular troubling drop-down list script on around the 18th last line.

Any suggestions for overcoming this obstacle is greatly appreciated!


<?

/* Display categories */

$query = "SELECT id,category,description from categories where id!=11 and deleted=0 order by category";

$result = mysql_query($query) or die("Query failed : " . mysql_error());

$last=0;

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

  if($last>0){

    ?>

    <!-- | -->

    <?

    }

    ?>

    <!--<a href="cat.php?id=<?= $line['id'] ?>" class="sub"><?= $line['category']

    ?></a>-->


    <?

    if($_GET['id']==$line['id'] || $last==0){

      $description=stripslashes($line['description']);

      $title=stripslashes($line['category']);

      $last=$line['id'];

    }

  }

  if(isset($_GET['id'])){

 $last=$_GET['id'];

}


mysql_free_result($result);




//get product details and convert them into local variables

$query = "SELECT * from products where deleted=0 and id=" . $_GET['id'];

$result = mysql_query($query) or die("Query failed : " . mysql_error());

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

  $name = stripslashes($line['name']);

  $description = stripslashes($line['description']);

  $retail = stripslashes($line['retail']);

  $price = stripslashes($line['price']);

  $bulb = stripslashes($line['bulb']);

  $redirect = stripslashes($line['redirect']);

  $ceiling = stripslashes($line['ceiling']);

  $dims = stripslashes($line['dims']);

  $static = stripslashes($line['static']);

}

mysql_free_result($result);

//determining if product has signature or radiance glass, also sets values for cap n pole image

$colorsetname = "Signature";

if ((isset($name))&&((substr($name,14,8)=="Radiance")||(substr($name,0,8)=="Radiance")))

	{$viewcol=5;

	$colorsetname = "Radiance";}

else {$viewcol=6;}

$ispendant = 16;

$redirviewcol = ($viewcol+999);


?>

              <table height="530" width="890" border="0" cellpadding="4" cellspacing="0">

              <tr>

                <td width="560" height="371" align="middle" valign="middle"> 

                  <img name="mainimg" id="mainimg" src="<? 


//get main image

$query = "SELECT * from glass where deleted=0 and pid=" . $_GET['id'] . " order by color limit 1";

$result = mysql_query($query) or die("Query failed : " . mysql_error());

 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

   echo $line['url'];

 }

mysql_free_result($result);

?>" >


<!-- width="263" height="353" -->

</td><? if(($_GET['id']==1004)||($_GET['id']==1005))

{echo '</tr><tr>';}?>

   <td valign="middle" align ="center"> 

    <table border="0" cellpadding="0" cellspacing="0">

       <tr>

         <td valign="top" align="center">


		 <? if($_GET['id']!=14){ ?>

		 <table width="239" border="0" cellspacing="0" cellpadding="0">

         

<?

$narrows=3;

if(($_GET['id']==1004)||($_GET['id']==1005))

{$narrows=5;}

//This is for the main views of the product colors

//get glass color

//This is for the alternate or installed views of the product in a separate table

$query = "SELECT * from glass where deleted=0 and pid=" . $_GET['id'] . " order by color";

$result = mysql_query($query) or die("Query failed : " . mysql_error());

if(mysql_num_rows($result)>1){

  echo '<tr valign="top">

  <td colspan="4"><span class="sub">Colors</span> 

  <span class="small">(click swatch to see item in that color glass)</span>';

if ($narrows==3)

{echo '<br />Click to order a <a href="pro.php?id='.$redirviewcol.'">'.$colorsetname.'</a> glass sample!</a></td></tr>';}

  else {echo'</tr>';}

  echo '<tr>';

  $cnt=0;

 

 //next three lines check for snapshot in the string

   while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { 

if(substr($line['color'],0,8)!="snapshot")

{ 

  {

    echo '<td><img src="' . $line['color_url'] . '" width="120" height="61" hspace="4" vspace="4" 

    border="0" onclick="change_glass(' . "'" . $line['color'] . "'" . ');document.mainimg.src=' . "'" . $line['url'] . "'" . '">

    </td>';

    $cnt++;

    $lookupcolor.=" if(col=='" . $line['color'] .  "'){ return '" . $line['url'] . "'; }\n";

	if($cnt % $narrows == 0){

      echo '</tr><tr>';

    }

  

  //next twelve lines check for snapshot in the string and do something

}

  }

}

}

mysql_free_result($result);

$query = "SELECT * from finishes where deleted=0 and pid=" . $_GET['id'] . " order by finish";

$result = mysql_query($query) or die("Query failed : " . mysql_error());

?>

</table></td></tr><tr><td align="center">

<?PHP

if($_GET['id']==201||$_GET['id']==202||$_GET['id']==204||$_GET['id']==205||$_GET['id']==206||$_GET['id']==207||$_GET['id']==30||$_GET['id']==31)

{echo '

	<table width="255" border="0" cellspacing="0">

       <tr valign="middle"> 

         <td align="center"><a href="javascript:void(viewer7.show(0))"><img src="Swatches/nickelpendantthumb.png" /></a></td>

		 <td align="center"><a href="javascript:void(viewer8.show(0))"><img src="Swatches/blackpendantthumb.png" /></a></td>

		 <td align="center"><a href="javascript:void(viewer9.show(0))"><img src="Swatches/rustpendantthumb.png" /></a></td>

		 <td align="center"><a href="javascript:void(viewer10.show(0))"><img src="Swatches/whitependantthumb.png" /></a></td>

       </tr></table>';

	   $ispendant = 4;}

elseif($_GET['id']==1030||$_GET['id']==1031||$_GET['id']==1032||$_GET['id']==1033)

{echo '

	<table width="255" border="0" cellspacing="0">

       <tr valign="middle"> 

        <td align="center"><a href="javascript:void(viewer11.show(0))"><img src="Swatches/nickeltrackpendantthumb.png" /></a></td>

		<td align="center"><a href="javascript:void(viewer12.show(0))"><img src="Swatches/blacktrackpendantthumb.png" /></a></td>

		<td align="center"><a href="javascript:void(viewer13.show(0))"><img src="Swatches/browntrackpendantthumb.png" /></a></td>

		<td align="center"><a href="javascript:void(viewer14.show(0))"><img src="Swatches/whitetrackpendantthumb.png" /></a></td>

       </tr></table>';

$ispendant=3;}

elseif(mysql_num_rows($result)>0)

{echo '<center><a href="javascript:void(viewer.show(0))">Click here to review metal color and finishes.</a></center>';}

else

{echo " ";}

 } //end id!=14 ?>


	</td>

  </tr>

</table>

</td>

</tr>

<tr>

<td valign="top" align="center">


		 <? if($_GET['id']!=14){ ?>

		 <table width="239" border="0" cellspacing="0" cellpadding="0">

         

<?

$narrows=5;

//This is for the alternate or installed views of the product in a separate table

$query = "SELECT * from glass where deleted=0 and pid=" . $_GET['id'] . " order by color";

$result = mysql_query($query) or die("Query failed : " . mysql_error());


   while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { 

if(substr($line['color'],0,8)=="snapshot")

{


if((mysql_num_rows($result)>1)){

  echo '<tr valign="top"> 

  <td colspan="4"><span class="sub">Installed or Alternate Views</span>';}

  else {echo'</tr>';}

  echo '<tr>';

  $cnt=0;

 

 //next three lines check for snapshot in the string


    echo '<td><img src="' . $line['color_url'] . '" width="120" height="61" hspace="4" vspace="4" 

    border="0" onclick="change_glass(' . "'" . $line['color'] . "'" . ');document.mainimg.src=' . "'" . $line['url'] . "'" . '">

    </td>';

    $cnt++;

    $lookupcolor.=" if(col=='" . $line['color'] .  "'){ return '" . $line['url'] . "'; }\n";

	if($cnt % $narrows == 0){

      echo '</tr><tr>';

    }

  

  //next twelve lines check for snapshot in the string and do something

}

}

}

mysql_free_result($result);

$query = "SELECT * from finishes where deleted=0 and pid=" . $_GET['id'] . " order by finish";

$result = mysql_query($query) or die("Query failed : " . mysql_error());

?>

</table></td></tr><tr><td align="center">

	</td>

</tr>

</tr>

<tr>

  <td height="77" colspan="2" valign="middle" border="0">

  <br /><a href="javascript:;"> </a></span> 


<b>


<!-- Product description -->

<?= $name ?></b><br><br>

<?= $description; ?>

<br />

<br />

<?= $redirect; ?>

<br />

<br />

<?= 

$nostr = "";

  $showdims = substr($dims, 0, 70);

  echo $showdims;

?>                     

<br />

<br />

<?= $bulb; ?>

<div id="notify_price"></div>

</td></td>

    <tr>

      <td align="center" colspan="2"><form name="form1" method="post" action="pro.php">

     	  <input type="hidden" name="action" value="add">

		  <input type="hidden" name="id" value="<?= $_GET['id']; ?>">

          <table border="0" cellspacing="10" cellpadding="0">

             <tr>

<?

$header_number=1;


//get glass color if id not equal to 14

if($_GET['id']!=14){ 

  $query = "SELECT * from glass where deleted=0 and pid=" . $_GET['id'] . " order by color";

  $result = mysql_query($query) or die("Query failed : " . mysql_error());

   if(mysql_num_rows($result)>1){

    echo '<td valign=top style="height: 75"><a href="javascript:void(viewer'.$viewcol.'.show(0))" style="text-decoration:none"><img src="images/Circle' . $header_number . '.png" class="nob">   <img src="images/GlassColor.png"></a><br /><select name="glass" onChange="document.mainimg.src=lookupcolor(document.form1.glass.value)">';

    $header_number++;

	

	

//the part of the code that changing the pictures is dependent upon and also creates the drop-down list of selections	

    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

	

	if(substr($line['color'],0,8)=="snapshot"){

      echo '<option value="' . $line['color']  . '">' . $line['color'] .  '</option>';

    }

	else

	{

      echo '<option value="' . $line['color']  . '">' . $line['color'] .  '</option>';

    }

	}

    echo '</select></td>';

	

  }


  mysql_free_result($result);

}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users