Jump to content

Omzy

Members
  • Posts

    314
  • Joined

  • Last visited

    Never

Everything posted by Omzy

  1. Basically I have a months array: $months=array('01'=>'Jan', '02'=>'Feb', '03'=>'Mar', '04'=>'Apr', '05'=>'May', '06'=>'Jun', '07'=>'Jul', '08'=>'Aug', '09'=>'Sep', '10'=>'Oct', '11'=>'Nov', '12'=>'Dec'); And I use a foreach loop to access this array and generate the markup as follows: echo '<option value="'.$index.'"'>'.$value.'</option>'; As you can see the <option> VALUE attribute is 2 digits, which is why I've had to create the array with the 2 digit numbers as the index. Can this be done in a more simpler way?
  2. The thing with that is, $options is a long string, I don't fancy using that as an array key. 'item_number' is just a 3 digit number so it's ideal to have as an array key..
  3. OK I managed to figure out the first two but the last one is now proving to be very difficult - any help would be appreciated. It's difficult because $_POST['item_number'] has to be unique, so I probably need to add on a counter or something..
  4. Here is my SESSIONS array: $_SESSION[$_POST['item_number']]=array($_POST['item_number'], $_POST['item_name'], $_POST['options'], $_POST['amount']); Somewhere in this array I want to include a $quantity variable - the way the site is designed there is currently no quantity field on the product pages so I want to be able to set this on the checkout page as follows: 1) If the $_POST['item_number'] is POSTed for the first time, $quantity is set to 1. 2) If the same $_POST['item_number'] is POSTed again, the $quantity of that session item is increased 3) If the same $_POST['item_number'] is POSTed again but the $options variable is different then a new session item is added to $_SESSION
  5. I'm now trying to add up values to get a subtotal: $subtotal=0; foreach($_SESSION as $value) { $total=number_format($value[2]*$value[3], 2); $subtotal=number_format($subtotal+$total, 2); } echo $subtotal; But this does not always display the correct value for $subtotal
  6. Cheers. The next thing I need to do is enable quantities to be changed. I got this to work when there is only 1 item but it doesn't seem to work when there is more than 1 item: foreach($_SESSION as $value) { <input type="text" name="quantity_update" value="'.$value[2].'" size="2"/> <input type="hidden" name="item_number" value="'.$value[0].'"/> } $_SESSION[$_POST['item_number']] = array($_POST['item_number'], $_POST['item_name'], $_POST['quantity'], $_POST['amount']); if(isset($_POST['quantity_update'])) { $_SESSION[$_POST['item_number']][2] = $_POST['quantity_update']; } I think I need to put it into an array or something, but how?
  7. $price=250.00; $quantity=2; $total=$price*$quantity; echo $total; //outputs 500 I want it to output 500.00
  8. What I'm looking to do is store multiple items in the SESSIONS array (the whole point of using sessions really). The items will be POSTed to cart.php, all items include fileds "item_name", "item_code", "item_price". How can I ensure the items are added uniquely to the SESSIONS array?
  9. Does anyone know how I can do this more elegantly, i.e. without having to hardcode the values in several IF conditions, and make it work out what the value of $total should be based on how many products there are? So if there are 7 products then it needs to know that the required multiple of 5 is 10.
  10. I fixed it myself: if($numrows<=5) { $total=5; } elseif($numrows<=10) { $total=10; } for($i=0; $i<$total; $i++) { if($i<$numrows) { //echo the product } else { //echo the blank LI } }
  11. What I'm trying to acheive is something very basic - a grid of products in rows of 5 - I could do this with tables but I'm trying to use semantic design concepts so I'm using UL/LI tags. I've got a CSS property of { li: border-top: 1px solid black }, so this basically displays a border at the top of the LI so that each row of 5 products is seperated by a border. So far so good, however the problem is when the total number of products is not a multiple of 5 then the row border is incomplete. Here's how I'm displaying the products (from a database): for($i=0; $i<$numrows; $i++) { echo "<li><a href=\".$products[$i]).".jpg\" \"><img src=\"images/products/thumbs/".$products[$i]).".jpg\" alt=\"\" /></a></li>"; } I think basically what I need is to display extra (blank) LI tags when the total number of products is not a multiple of 5. I tried putting in a conditional IF statement within the FOR loop and then changing $i<$numrows to $i<5 || $i <10 but this messes up the display. Anyone got any more ideas?
  12. Cheers but like I said I cant echo the <script> above the while loop becuase I have more JavaScripts like this, if I echo'd them all at the top the'd just be nested within each other.
  13. This is probably really easy but it's been a while since I touched PHP, so any advice would be appreciated. Basically what I want to do is output dynamic data from the database: $query="SELECT * FROM products WHERE cat_id=$cat_id AND"; $results = mysql_query($query); $numrows = mysql_num_rows($results); while($row=mysql_fetch_assoc($results)) { $prod_id=$row['prod_id']; $name=$row['name']; } echo "<script language="javascript"> "; echo "var i=0; "; echo "var roll_images=new Array; "; for($i=0;$i<$numrows;$i++) { $array[$i]=$prod_id; } for($i=0;$i<$numrows;$i++) { echo "roll_images[".$i."]="images/r/".$array[$i].".jpg"; "; } echo "</script> "; This is just a snippet of the code, I've removed most of the stuff so that you get a better idea of what's going on. As you can see I've echo'd the <script> tag outside of the WHILE loop to stop it from being output every time it loop, however I need the internal FOR loop to run within the WHILE loop. How can this be achieved? I cant move the <script> tag above the WHILE loop because I have other scripts that also need to be put in the WHILE loop.
  14. That's what it's already doing. Read the post properly.
  15. No Frameworks, no templating systems. Just a short piece of code that will do the job for me, I hope!
  16. Basically I just need a quick, easy and simple way of separating my PHP code from my HTML code. So I wanna have one file with all the HTML and one file with the PHP code, linked via an include. I'm not after a whole templating system like Smarty, just something simple that I can code by myself. Are there any online tutorials on how to do this?
  17. I got an application which has two states, as follows: State 1) index.php?action=add if((isset($_POST['submit'])) || (isset($_GET['action']) && $_GET['action']=='add')) On this page there is a form, when you submit the form it goes back to this state and displays a "confirmation" message. if(isset($_POST['submit']) && empty($errors)) { //run the query //display the message } If there was an error in the form it re-displays the form with an error message. State 2) index.php?action=display if(isset($_GET['action']) && $_GET['action']=='display') On this page the added entries are displayed I want to change the functionality now so that it displays state 2 when an entry has been successfully added, with a "confirmation" message. If there was an error in the submitted data it re-displays the previous form. Can this be acheived without using a header() function?
  18. By secure I don't mean military level security, just general security... This is what I currently insert at the top of my PHP scripts: $server="server"; $user="username"; $pass="password"; $connect=mysql_connect($server,$user,$pass); mysql_select_db("database"); Is this secure enough? Should I ought to use a different method?
  19. Lol yeah I just came across that last night, I had previously been doing it with mysql_fetch_array($results, MYSQL_ASSOC) but I've now changed all my code to mysql_fetch_assoc. Anyway I assumed the second method would be faster as all the variables are being assigned within the while statement?
  20. $query="SELECT * FROM customers ORDER BY id DESC LIMIT 5"; $results = mysql_query($query); while($row=mysql_fetch_assoc($results)) { $id=$row['id']; $name=$row['name']; $description=$row['description']; //echo the data } OR $query="SELECT id, name, description FROM customers ORDER BY id DESC LIMIT 5"; $results = mysql_query($query); while(list($id,$name,$description)=mysql_fetch_row($results)) { //echo the data } Also with regards to method 1, is it advisable to specify the field names in the query rather than selecting all fields?
  21. OK basically there are 4 parts on this form - parts 1-3 capture the data and part 4 is the confirmation page and where the email gets sent. At the moment I use $_POST to pass the data for each part of the form, so on parts 2 and 3 I still have to pass forward the data for part 1 using hidden variables. Am I right in understanding if I use sessions I only have to pass this data "once"?
×
×
  • 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.