Jump to content

PHP Array question


wmguk

Recommended Posts

Hey Guys,

 

I had a script written for me and im trying to work out what is happening with it.

 

<input name="addbox" type="button" value=" + " onClick="Item('<?php echo $row['refnum'];?>','<?php echo $row['price'];?>','<?php echo $row['descr'];?>','CL_<?php echo $row['refnum'];?>','add','cl')"> 
<input type="button" name="subbox" value="  - " onClick="Item('<?php echo $row['refnum'];?>','<?php echo $row['price'];?>','<?php echo $row['descr'];?>','CL_<?php echo $row['refnum']; ?>','delete','cl')"> <span class=formnames><strong>
<input type="text" name="cl<?php echo $row['refnum'];?>" value="None" size="6" readonly STYLE="background-color:#ffffff;border:0;font-weight:bold;color:#666666;"></strong></span></td>
<td align="center" width="158"> 
<input name="addbox" type="button" value=" + " onClick="Item('<?php echo $row['refnum'];?>','<?php echo $row['price'];?>','<?php echo $row['descr'];?>','BW_<?php echo $row['refnum'];?>','add','bw')"> 
<input type="button" name="subbox" value="  - " onClick="Item('<?php echo $row['refnum'];?>','<?php echo $row['price'];?>','<?php echo $row['descr'];?>','BW_<?php echo $row['refnum']; ?>','delete','bw')"> <span class=formnames><strong>
<input type="text" name="bw<?php echo $row['refnum'];?>" value="None" size="6" readonly STYLE="background-color:#ffffff;border:0;font-weight:bold;color:#666666;"></strong></span></td></tr>

 

and im displaying this:

 

<?php echo $item_description[$order_items[3*$i+1]]; ?>

 

and im confused as to how show other items....

 

any thoughts?

Link to comment
Share on other sites

hmmm the pages are SOOOOO complex!

 

basically its a veru easy slimline shopping basket system

 

You view the item, select the item with + / - buttons and then view basket.

 

the items are then displayed in the basket, however what is happening is the items are showing the correct prices etc, but the wrong names...

 

this is the display item page (works correctly, and seems like the infomation is correct.

 

  <table width="570" border="0" align="left" cellpadding="0" cellspacing="0">
    <tr>
    	<td colspan="2"><span class="header">
<? 
  // Get the album name
include '../scripts/connection.php';
$result = mysql_query("SELECT * FROM album WHERE login = '$login'");
while($row = mysql_fetch_array($result)) 
{ 
$albumname = $row['albumname']; 
$pricing = $row['pricing']; 
$creator = $row['creator'];
echo $albumname ;
}
?>
    	</span></td>
   	  </tr>
    <tr>
      <td align="centre" valign="top"><img onload="LoadOrder('<?php echo $image_id;?>')" src="<?php echo $im_id ; ?>" border="1" width="400" style="border-color:#000000"/></td></tr>
    <tr><td><div align="center">
    <a href="bw.php?filename=<?php echo $image_id ; ?>&login=<? echo $login; ?>&pricing=<? echo $pricing; ?>&image=<? echo $image_id; ?>">
    <img src="../images/order/bw.gif" width="80" height="80" hspace="5" border="0" /></a>
    <a href="view_order2.php?id=<? echo $login; ?>&image_id=<? echo $image_id; ?>&creator=<? echo $creator; ?>&passcode=<?php echo $passcode; ?>&pricing=<?php echo $pricing; ?>">
    <img src="../images/order/view.gif" alt="" width="80" height="80" hspace="5" border="0"/></a>
    </div>            
    </td></tr>
    <tr><td colspan="2" align="left" valign="top">
	<?php
include('../order/school_selection.php'); 
    	?></td></tr></table>

 

the include page is:

 

<script type='text/javascript' language='javascript'>

  var escape_Char='##';

function modify_string_value(string,escape_Char,url,code,act) {
var quan;
var item_string = url+escape_Char+code+escape_Char;
var begin = string.indexOf (item_string, 0);
if(begin==-1 && act=='delete'){ return null; }
if(begin==-1 && act=='add'){
	string=string+url+escape_Char+code+escape_Char+'1'+escape_Char;
	return string;
} else {
  var quan= string.substring(begin+item_string.length,string.indexOf(escape_Char,begin+item_string.length));
  quan=parseInt(quan);

 if(act=='delete' && quan==1){
	string=string.substring(0,begin) +string.substring(begin+item_string.length+1+escape_Char.length);
	return string;
 } else {
	if (act=='delete') {quan= quan-1; }
	if (act=='add') {quan= quan+1; }

	string = string.substring(0,begin) + url+escape_Char+code+escape_Char+quan+escape_Char +string.substring(begin+item_string.length+1+escape_Char.length);
	return string;
	//alert(string);
 }
}
}

function getCookie (name) {
var dcookie = document.cookie; 
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
        while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
                if (dcookie.substring(cbegin, vbegin) == cname) {
                var vend = dcookie.indexOf (";", vbegin);
                        if (vend == -1) vend = clen;
                return unescape(dcookie.substring(vbegin, vend));
                }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
                if (cbegin == 0) break;
        }
return null;
}

function modify_form_value(code,act)
{
var found = false;
var i = 0;
while ((found == false) && (i < document.form1.elements.length))
  {
    i = i + 1;
   if (document.form1.elements[i].name == code)
     {
      found = true;
if(document.form1.elements[i].value == 'None' && act == 'add'){
      		document.form1.elements[i].value = 1; //
} else {
   if(act == 'delete'){

	if(parseInt(document.form1.elements[i].value)-1>0){
     		document.form1.elements[i].value = parseInt(document.form1.elements[i].value)-1;
	} else {
		document.form1.elements[i].value = 'None';
	}
} else {
	document.form1.elements[i].value = parseInt(document.form1.elements[i].value)+1;
   }
}
     }
  }
}


function parser(){
var order = document.cookie;
}



function Item(code,price,desc,url,act,type)
{

  var order_cookie=getCookie ('order');
  var mod;
  var dcookie;
  var fld;
  fld= type+code;
  if (order_cookie == null || order_cookie.length ==0){
   if ( act == 'add') {
    dcookie="order="+ escape(url+escape_Char+code+escape_Char+'1'+escape_Char);
    modify_form_value(fld,act);
    document.cookie = dcookie;
    return 1;
   }
   return 1;
  }
  mod = modify_string_value(getCookie ('order'),escape_Char,url,code,act);
  if (!(mod == null)){
   document.cookie ="order="+ escape(mod);
  }

  modify_form_value(fld,act);
}

function LoadOrder(url){
  var pos=0;
  var pos_new;
  var code;
  var quan;
  var order_cookie=getCookie ('order');
  if (order_cookie == null || order_cookie.length ==0){ return 1;}
  while ( order_cookie.indexOf(url, pos) > -1 ) {
pos_new = order_cookie.indexOf(url, pos);
code=order_cookie.substring(pos_new+url.length + escape_Char.length,order_cookie.indexOf(escape_Char,pos_new+url.length + escape_Char.length));

quan=order_cookie.substring(pos_new+url.length + 2*escape_Char.length+code.length,order_cookie.indexOf(escape_Char,pos_new+url.length + 2*escape_Char.length+code.length));
for (var i=0;i<quan;i++){
	modify_form_value(code,'add');
}
pos=pos_new+url.length;
  }
}

</script>

<!--GET CURRENCY -->
<?php 
include "../scripts/connection.php";  
if (!$con)  
{  
  die( 'Could not connect: ' . mysql_error() );  
} 
mysql_select_db($db, $con); 

$sql = "SELECT * FROM currency"; 
$result = mysql_query( $sql ) or die( "<strong>Query Error</strong>: " . mysql_error() . "<br><strong>Query</strong>: $sql<br><br>" );$row = mysql_fetch_assoc($result); 
$cur = $row['cur']; 
$name = $row['name'];
$symbol = $row['symbol'];
		   ?>
<!--BEGIN PRODUCT/SIZE SELECTOR -->
<body onLoad="document.cookie=null;">
<TABLE width="549" border="0" cellpadding="0" cellspacing="0">
  <tr><td align="center">
<span class=formnames><br><br><strong>Please use the + / - buttons below to select which items you wish to order.</strong><br>Prices indicated below are in <? echo $name ;?> [<? echo $cur ;?>]<br></span>
<form name=form1 id=form1>
        <table border="1" width="538" CELLPADDING="2" CELLSPACING="0">
          <tr> 
            <td width="162" align="center" valign="middle" class="formnames"><strong>Item 
              Description</strong></td>
            <td width="40" align="center" valign="middle" class="formnames"><strong>Price</strong></td>
            <td width="152" align="center" valign="middle" class="formnames"><strong>Quantity Selected[Color]</strong></td>
            <td width="158" align="center" valign="middle" class="formnames"><strong>Quantity Selected[b&W]</strong></td>

</tr>
<?php
$pricing = str_replace("+", " ", "$pricing");

$query="SELECT * FROM pricing WHERE prodname = '$pricing' ORDER BY refnum ASC";
$result=mysql_query( $query , $con ) or die( "<strong>Query Error</strong>: " . mysql_error() . "<br><strong>Query</strong>: $query<br><br>" );
while ($row = mysql_fetch_array($result)) {
?>

<tr>
<td width="162" align="left"><span class=formnames><?php echo $row['descr']; ?></span></td>
<td align="center" width="40"><span class=formnames><? echo $symbol ;?><?php echo $row['price'];?></span></td>
<td align="center" width="152"> 
<input name="addbox" type="button" value=" + " onClick="Item('<?php echo $row['refnum'];?>','<?php echo $row['price'];?>','<?php echo $row['descr'];?>','CL_<?php echo $row['refnum']; ?>','add','cl')"> 
<input type="button" name="subbox" value="  - " onClick="Item('<?php echo $row['refnum'];?>','<?php echo $row['price'];?>','<?php echo $row['descr'];?>','CL_<?php echo $row['refnum']; ?>','delete','cl')"> <span class=formnames><strong>
<input type="text" name="cl<?php echo $row['refnum'];?>" value="None" size="6" readonly STYLE="background-color:#ffffff;border:0;font-weight:bold;color:#666666;"></strong></span></td>
<td align="center" width="158"> 
<input name="addbox" type="button" value=" + " onClick="Item('<?php echo $row['refnum'];?>','<?php echo $row['price'];?>','<?php echo $row['descr'];?>','BW_<?php echo $row['refnum'];?>','add','bw')"> 
<input type="button" name="subbox" value="  - " onClick="Item('<?php echo $row['refnum'];?>','<?php echo $row['price'];?>','<?php echo $row['descr'];?>','BW_<?php echo $row['refnum']; ?>','delete','bw')"> <span class=formnames><strong>
<input type="text" name="bw<?php echo $row['refnum'];?>" value="None" size="6" readonly STYLE="background-color:#ffffff;border:0;font-weight:bold;color:#666666;"></strong></span></td></tr>
<?php
}
?>
</table>
</form>
<!-- END PRODUCT/SIZE SELECTOR -->
</td></tr>
</TABLE>
</body>

 

then the basket page: - i've stripped away a  lot of code, as I dont think i need to display it for you (its confusing!)

 

<tr>
<td width="150">     </td>
<td width="100" align=center><span class=formnames><strong>Image ID</strong></td>
<td width="100" align=center><span class=formnames><strong>Item Description</strong></td>
<td width="75" align=center><span class=formnames><strong>Price</strong></td>
<td width="50" align=center><span class=formnames><strong>Colour Quantity</strong></td>
<td width="50" align=center><span class=formnames><strong>B&W Quantity</strong></td>
<td width="75" align=right><span class=formnames><strong>Sub Total</strong></td>
</tr>

<tr>
<td colspan=7><HR color="#666666"></td>
</tr>
<?php
$query="SELECT * FROM pricing ORDER BY 'price' ASC";
$query2="SELECT * FROM album WHERE login = '$login'";
$query4="SELECT * FROM our_info WHERE username = '$creator'";

$result=mysql_query($query , $con) or die( "<strong>Query Error:</strong>" . mysql_error() . "<strong>Query</strong>: $query" );	
$result2=mysql_query($query2 , $con) or die( "<strong>Query Error:</strong>" . mysql_error() . "<strong>Query</strong>: $query2" );
$result4=mysql_query($query4 , $con) or die( "<strong>Query Error:</strong>" . mysql_error() . "<strong>Query</strong>: $query4" );

while($row = mysql_fetch_array($result2)) 
{ 
$postage = $row['postage']; 
}

if ($postage == 'yes') {
while($row = mysql_fetch_array($result4)) 
{ 
$packcost = $row['packcost']; 
}
} else {
$packcost = '0';
}

// retrieving the information from the pricing table
while ($row = mysql_fetch_array($result)) {
	$item_price[$row['refnum']]=$row['price'];
	$item_description[$row['refnum']]=$row['descr'];
	$item_weight[$row['refnum']] = $row['weight'];
}

$total=0;
$totalWeight = 0;
$totalQuantity = 0;
$order_items=split($escape_char, $_COOKIE["order"]);
for($i=0;$i<intval(sizeof($order_items)/3 );$i++){
	$imageFile = $order_items[3*$i];
	$type = explode("_", $imageFile);
	$link = "passcode.php";
	$src= $order_items[3*$i];
	$colorType = "";
	//echo $type[0];
	if($type[0] == "BW" || $type[0] == "SEPIA") {
		$colorType = $type[0];
		unset($type[0]);
		$order_items[3*$i] = implode("_", $type);
		$link = strtolower($colorType) . ".php";
		$src= "http://www.iwphoto.co.uk/images/school/".$_GET['id']."/". $_GET['image_id'];
	}
		$column1 = '<td width="150"><img src="' . $src .'" width="100" border="0" align="top" style="border-style:outset; border-color: black; border-width:1px;"></td>';
?>
 <tr>
  <td width="150"><img src="<?php echo 'http://www.iwphoto.co.uk/clients/'.$_GET['id'].'/'.$_GET['image_id']; ?>" width="100" border="0" align="top" style="border-style:outset; border-color: black; border-width:1px;"></td>
  
  <td width="100" align=center><span class=formnames><strong><?php echo $_GET['image_id']; ?></strong></td>
      <td width="100" align=center><span class=formnames><strong><?php echo $item_description[$order_items[3*$i+1]]; ?></strong></td>
  <td width="75" align=center><span class=formnames><strong><?php echo $item_price[$order_items[3*$i+1]]; ?></strong></td>
<?php if($colorType=='BW')
 {	 ?> 
	  <td width="50" align=center><span class=formnames><strong> </strong></td>
	  <td width="50" align=center><span class=formnames><strong><?php echo $order_items[3*$i+2]; ?></strong></td>
 <?php }
 else
 { ?>
 	<td width="50" align=center><span class=formnames><strong><?php echo $order_items[3*$i+2]; ?></strong></td>
	<td width="50" align=center><span class=formnames><strong> </strong></td>
 <?php } ?>
  <td width="75" align=right><span class=formnames><strong><?php printf("%02.2f",$order_items[3*$i+2]*$item_price[$order_items[3*$i+1]]); $total=$total+$order_items[3*$i+2]*$item_price[$order_items[3*$i+1]]; $totalWeight = $totalWeight + $order_items[3*$i+2]*$item_weight[$order_items[3*$i+1]]; $totalQuantity = $totalQuantity + $order_items[3*$i+2];?></strong></td>
 </tr><tr>
<td colspan=7><HR color="#666666"></td>

Link to comment
Share on other sites

There are lot of things are coming from cookies and code is confusing. Why it does multiply with 3, I don't get it. But the problem is perhaps in the math signs order

 

instead of this

 

$item_description[$order_items[3*$i+1]]

 

you should use this perhaps.

 

$item_description[$order_items[3*($i+1)]]

 

now the items are increasing with the multiples of 3 (first add 1 to $i than multiply with 3). Former was increasing as multiples of 4 (first multiply with $i than add 1)

 

maybe this helps.

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.