SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: Arrays?

  1. #1
    SitePoint Enthusiast gcarlyle's Avatar
    Join Date
    Mar 2002
    Location
    Olympia Wa
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Arrays?

    Hi all,

    hopefully this is a simple question.

    after selecting data from the db such as this, how do I display everything to the browser?

    PHP Code:
    $query "SELECT id, image, name, quantity, price FROM {$config["prefix"]}_items where id = 1";
    $ret mysql_query($query); 
    I know it has to be in array form but I am not yet familure with them.

    Thank for all your help.
    And thanks in advance.
    Jennifer
    Wanna be php coder!

  2. #2
    Wibblesticks Gryff's Avatar
    Join Date
    Oct 2001
    Location
    Asgard
    Posts
    442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $blah = mysql_fetch_array($ret);

    you can then use
    echo("$blah[0]");
    for return the first element of the array and [1] [2] etc. and so on
    you can also use
    echo(" $blah[id] ");
    to echo a specific element if thats what you want
    its easier to do that than remember the order of your array, but a bit longer.

  3. #3
    SitePoint Addict
    Join Date
    Feb 2002
    Location
    Atlanta, GA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know it has to be in array form but I am not yet familure with them.
    Make sure you more than familiar with arrays, you want to ideally master them, at the bare minimum become comfortable. In particular, get to know nested arrays, which are arrays that hold other arrays, also called multi-dimensional arrays.

    The reason you want to do this is because once you're confident with arrays you'll find all sorts of uses for them, your code will be cleaner and more robust.

    Oh, did I mention, the opposite sex will find you irrresistible, especaially when you go out with that big A on your chest!


    Good luck,

    Pete


  4. #4
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Gryff
    $blah = mysql_fetch_array($result);

    you can then use
    echo("$blah[0]");
    for return the first element of the array and [1] [2]
    technically, mysql_fetch_array() would return an associative array without specifying the optional 2nd parameter (read the reference) so you cannot access it via numeric indices... you can access them as such:
    PHP Code:
    while($row=mysql_fetch_array($result))  // this will loop thru every row
    {
      echo 
    $row['image'];  // echo the image field
      
    echo $row['name'];  // echo the name field
      // you can so the same for the other fields

    you'd can also use the mysql_fetch_row() or mysql_fetch_object() functions instead... mysql_fetch row() returns a numeric array while mysql_fetch_object() returns an object...

  5. #5
    SitePoint Enthusiast gcarlyle's Avatar
    Join Date
    Mar 2002
    Location
    Olympia Wa
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I have it tell me how I did. I mean this works but if there is an easier way or a way to trim it down let me know.

    PHP Code:
    $query "SELECT id, image, name, quantity, price FROM {$config["prefix"]}_items where id = 1";
    $result mysql_query($query);
    $row mysql_fetch_array($result);
    echo (
    "<form method=post action=\"holoshop.php\">");
    echo 
    "<input type='image' src='images/redholo.gif'><br>";

    echo 
    "<b>$row[name]</b><br>";
    echo 
    "Quantity $row[quantity]<br>";
    echo 
    "Cost: $row[price] SC<br>";

    echo (
    "<input type=\"hidden\" name=\"ItemID\" value=\"$id\">");
    echo (
    "<input type=\"hidden\" name=\"action\" value=\"update\">");
    echo (
    "</form><p>");

    if (
    $HTTP_POST_VARS[action] == "update") {
    $quantity $row[quantity] - 1;
    $query2 =("UPDATE {$config["prefix"]}_items SET quantity = '$quantity' WHERE id = '1'");
    mysql_query($query2);
    echo (
    "done");

    And now this brings me to another question. If I have 2 items with different id's say 1 and 2 and the image is clicked both quantities are updated instead of just the one clicked. I'd hate to have to write out a different page for each item sold.

    Is there a way I can do this with having the variables passed to one page 'buy.php' instead of having multiple buy.php pages?

    Thanks

    Jennifer
    Wanna be php coder!

  6. #6
    Wibblesticks Gryff's Avatar
    Join Date
    Oct 2001
    Location
    Asgard
    Posts
    442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    redemption
    sorry, but your wrong
    I qoute from the manual page you referenced

    *
    mysql_fetch_array -- Fetch a result row as an associative array, a numeric array, or both.
    *
    further down

    *
    The optional second argument result_type in mysql_fetch_array() is a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. This feature was added in PHP 3.0.7. MYSQL_BOTH is the default for this argument.
    *


    gcarlyle
    heres some neater code
    PHP Code:
    // I used SELECT * in case you want extra stuff later
    // It will also speed the code up a little bit and 
    // shorten filesize
    $row mysql_fetch_array(mysql_query("SELECT * FROM {$config["prefix"]}_items where id = 1")); 
    Your echo stuff is basically ok

    PHP Code:
    if ($_POST[action] == "update") {
    $quantity $row[quantity] - 1;
    mysql_query("UPDATE {$config["prefix"]}_items SET quantity = '$quantity' WHERE id = '1'");
    echo (
    "done");


    $_POST replaced $HTTP_POST_VARS a few updates back

  7. #7
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    whoops my bad... apologies to Gryff

    anyway, if you're just gonna use the numeric indices to access your fields, use mysql_fetch_row() for a smaller memory usage (because of the smaller array size)... but yeah like Gryff has pointed out you can use either... sorry Gryff

    as for the $_POST array, just a note of warning: if your version is PHP is earlier than 4.1.0, then you would have to use $HTTP_POST_VARS instead since $_POST is only available on PHP versions 4.1.x

  8. #8
    SitePoint Enthusiast gcarlyle's Avatar
    Join Date
    Mar 2002
    Location
    Olympia Wa
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That works for me thank you.

    I did find out the hard way that $_POST wouldn't work so I left it at $HTTP_POST_VARS.

    Thanks again
    Jennifer
    Wanna be php coder!

  9. #9
    SitePoint Enthusiast gcarlyle's Avatar
    Join Date
    Mar 2002
    Location
    Olympia Wa
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any way you can help with the second question?

    I have 2 items for sale. If one item is bought it updates both items and subtracts from the quantity of both when only one was bought.

    Is there a way to do this without having to write a buy.php for all items?

    I thought the id would take care of that but php parses all the code and I am at a loss on how to accomplish it.
    Wanna be php coder!

  10. #10
    Wibblesticks Gryff's Avatar
    Join Date
    Oct 2001
    Location
    Asgard
    Posts
    442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah sure
    if you pm me or something as i'm not quite sure what your doing with the id thing, maybe we can hook up on irc or msn.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •