Jump to content

[SOLVED] Can this code be optimised?


ukphoto

Recommended Posts

The following code works perfectley OK but do I really need the 3 lines from $RealResult to $ExplodeArray or is there a simpler way of doing this?

 

<?php
include 'testconOrders.php';
$Cust_Name = $_POST['Customer_Name'];
$Query=("SELECT `Array` FROM tbl_orders Where `CustomerName` = '$Cust_Name' ");
$Result = mysql_query($Query)or die(mysql_error());

$RealResult = mysql_fetch_assoc($Result);
$ResultString=$RealResult['Array'];
$ExplodeArray = unserialize($ResultString);

if (isset($ExplodeArray)){ 
   echo'The images ordered for '.$Cust_Name.' are as follows;<br><br>'; 
   foreach($ExplodeArray as $k => $v){ 
      echo 'Image: '.$k.' Qty: '.$v.'<br>'; 
   } 
}
else echo'No order for '.$Cust_Name.' or nill order sent';
?>

 

Mnay thanks

Link to comment
Share on other sites

Here it is a little shorter, but there isn't much more you can do:

 

<?php
  include 'testconOrders.php';
  $Result = mysql_query("SELECT `Array` FROM tbl_orders Where `CustomerName` = '".mysql_real_escape_string($_POST['Customer_Name'])."' LIMIT 1")
    or die(mysql_error());
  $Row = mysql_fetch_assoc($Result);
  if($ExplodeArray = unserialize($Row['Array'])){
    echo "The images ordered for {$Row['CustomerName']} are as follows;<br><br>"; 
    foreach($ExplodeArray as $k => $v)
      echo "Image: {$k} Qty: {$v}<br>";
  }else
    echo "No order for {$_POST['Customer_Name']} or nill order sent";
?>

Link to comment
Share on other sites

Rhodesa,

I realised that I may have to look back over more than one record so have amended the code to list the orders from newest to oldest i.e DESC order. I have left out the LIMIT, would it have any use here now?

 

<?php
include 'testconOrders.php';
$Result=mysql_query("SELECT `Array`, `Index` FROM tbl_orders Where `CustomerName` = '".mysql_real_escape_string($_POST['Customer_Name'])."' ORDER BY `Index` DESC ")
or die(mysql_error());
echo"The images ordered for {$_POST['Customer_Name']} are as follows;<br><br>";
print "<table border cellpadding=3>";
while ($Rows =  mysql_fetch_array($Result))
if ($ExplodeArray=unserialize($Rows['Array'])){ 
   print "<tr><th>Image:</th><th>Quantity:</th>";
   foreach($ExplodeArray as $k => $v)
    print "<tr><td>Image: {$k}</td><td> Qty: {$v}</td></tr>"; 
}
print "</table>"
?>

Link to comment
Share on other sites

almost there...you need braces for your while loop. you can also leave Index out of the SELECT fields:

 

<?php
  include 'testconOrders.php';
  $Result=mysql_query("SELECT `Array` FROM tbl_orders Where `CustomerName` = '".mysql_real_escape_string($_POST['Customer_Name'])."' ORDER BY `Index` DESC ")
    or die(mysql_error());
  if(mysql_num_rows($Result)){
    echo"The images ordered for {$_POST['Customer_Name']} are as follows:<br><br>";
    print "<table border cellpadding=3>";
    while ($Row =  mysql_fetch_array($Result)){
      if ($ExplodeArray=unserialize($Row['Array'])){ 
        print "<tr><th>Image:</th><th>Quantity:</th>";
        foreach($ExplodeArray as $k => $v)
          print "<tr><td>Image: {$k}</td><td> Qty: {$v}</td></tr>";
      }
    }
    print "</table>";
  }else
    echo "Customer {$_POST['Customer_Name']} has no orders;
?>

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.