SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Earlier this week, Freddydoesphp showed me how to put mysql results into an array.

    However I need to know how to pull one item out of an array.

    Code:
    while ($select = mysql_fetch_array($news)) {
    	$items[] = array("aid" => $select["ID"],
    					 "title" => $select["Title"],
    					 "name" => $select["Author"],
    					 "status" => $select["Status"]
    					 );
    
    	}
    Now, say I want to just get the title and do:
    Code:
    if($title_thing == "blah") {
    blah
    } else {
    boo
    }
    How would i Do this?


    Thank you
    Last edited by petesmc; Mar 30, 2001 at 16:07.

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pete,

    If you want to use the id number form the reocrd as the key for the array that is fine like below:

    while ($select = mysql_fetch_array($news)) {
    $items[$select["ID"]] = array("aid" => $select["ID"],
    "title" => $select["Title"],
    "name" => $select["Author"],
    "status" => $select["Status"]
    );

    }


    Now if you know the id number, simply call

    if ($items[$id]["title"] == "blah") {
    do something
    }
    else {
    do something else
    }



    Or else you will need to loop through the array looking for the correct array element and compare the title like this

    foreach($items as $key => $val) {
    if ($items[$key]["title"] == "blah") {
    do something
    }
    else {
    do something else
    }
    }

    Is this what you are after?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Exactly!

    See, I pull out 1 record of data from one table and the rest from aother table and I need to get a few variables from the first to get my script working.

    Thank you so much,
    Peter

  4. #4
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heres some other code, at the part where I'm trying to replace text with other text isn't working: Do you know why?


    <?php
    include("config.php");

    $get_guestbook = "SELECT data.ID as ID, data.Name as Title, header, format, footer, records_per_page, " .
    " allow_posting, allow_multiple, require_name, require_email, require_content, admin_name, admin_email, " .
    "guestbook.ID as PostID, GID, Email, Homepage, Content, Date, guestbook.Name as Name " .
    "FROM data, guestbook WHERE data.ID=GID AND data.ID=$id ORDER BY Date DESC";


    $do_get = mysql_query($get_guestbook);
    if (!$do_get) {
    print "At this time the guestbook cannot be located.";
    } else {
    while ($result = mysql_fetch_array($do_get)) {


    $items[] = array("ID" => $result["ID"],
    "Title" => $result["Title"],
    "Header" => $result["header"],
    "Footer" => $result["footer"],
    "Format" => $result["format"],
    "Records_per_page" => $result["records_per_page"],
    "allow_posting" => $result["allow_posting"],
    "allow_multiple" => $result["allow_multiple"],
    "require_name" => $result["require_name"],
    "require_email" => $result["require_email"],
    "require_content" => $result["require_content"],
    "admin_name" => $result["admin_name"],
    "admin_email" => $result["admin_email"],
    "PostID" => $result["PostID"],
    "GID" => $result["GID"],
    "Email" => $result["Email"],
    "Content" => $result["Content"],
    "Date" => $result["Date"],
    "Homepage" => $result["Homepage"],
    "Name" => $result["Name"]);

    }

    for($i=1;$i<2;$i++) {

    printf('%s', $items[$i]["Header"]);

    }

    ?>
    <hr>
    <?php

    foreach($items as $key => $val) {
    // HERE ############################
    $Format = $items[$key]["Format"];
    $Format = str_replace("{Date}", $Date, $Format);
    $Format = str_replace("{Email}", $Email, $Format);
    $Format = str_replace("{Homepage}", $HomepageDate, $Format);
    $Format = str_replace("{Name}", $Name, $Format);
    $Format = str_replace("{Content}", $Content, $Format);
    printf('%s', $items[$key]["Format"]);
    // END HERE ##########################
    }
    ?>
    <hr>
    <?php


    for($i=1;$i<2;$i++) {

    printf('%s', $items[$i]["Footer"]);

    }

    } ?>


    Thanx

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are printing the original value in the array:
    printf('%s', $items[$key]["Format"]);

    Don't you want to print $Format?
    printf('%s', $Format);

    Also, (minor point), but if all you are printing is a string, no need to use printf, use print

    print($Format);

  6. #6
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankyou very much, it is working now.

    -Peter


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
  •