Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts

    Best way to extract database array

    I need to find way to extract an array of users from database, but extract() function doesn't work like i thought it will

    I'm using simple way to loop through database, but can't find simple way to return that array so i can use it later.

    Code:
    PHP Code:
    public function statistike() {
        
            
    $sql = ("SELECT * FROM korisnici");
            
    $result mysql_query($sql);
                    
            while (
    $podaci mysql_fetch_assoc($result)) {
                echo 
    $podaci["id"]. "  ";
                echo 
    $podaci["ime"]. "  ";
                echo 
    $podaci["email"]."<br>";
                
                
    extract($podaci);
            }

            
    mysql_free_result($result);
            
            
    print_r($ime);
            
            return 
    $podaci;
        } 
    i read PHP manual, and function extract should give me arrays $ime ($name) $id and $email, but it doesnt work.

    for now i'm using array_push, but i thought it would be possible to get array of names by using extract, but looks like it doesn't work

  • #2
    Regular Coder Stooshie's Avatar
    Join Date
    Mar 2008
    Location
    Dundee, Scotland
    Posts
    378
    Thanks
    9
    Thanked 39 Times in 39 Posts
    You are doing the extract within the while loop, so it is being over-written each iteration.

    Do the print_r($ime) inside the while loop.

    Actually $ime won't be an array so you will only need to echo it.
    Regards, Stooshie
    O

  • #3
    Regular Coder logictrap's Avatar
    Join Date
    Apr 2008
    Posts
    155
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Try something like this:
    PHP Code:
    public function statistike() { 
         
            
    $sql = ("SELECT * FROM korisnici"); 
            
    $result mysql_query($sql); 
                     
            while (
    $podaci mysql_fetch_assoc($result)) { 
                
    $myarray()= $podaci
                 
                 
            } 

            
    mysql_free_result($result); 
             
            
    print_r($myarray); 
             
            return 
    $myarray
        } 

    Which came first - the chicken or the egg? The egg... [ticket closed]
    If a tree falls... does it make a sound? Yes.............. [ticket closed]

  • #4
    Regular Coder Stooshie's Avatar
    Join Date
    Mar 2008
    Location
    Dundee, Scotland
    Posts
    378
    Thanks
    9
    Thanked 39 Times in 39 Posts
    funnymoney, Is there any particular reason you need them as separate variables?

    Logictrap is right in that the user details will be easier to handle as a two dimensional array.

    "extract()" just turns an array into a set of vars. (eg $myarray["mykey"] = "myvalue" just becomes $mykey = "myvalue").
    Regards, Stooshie
    O

  • #5
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    i'm using .inc file to populate it with variables using pregmatch.

    .inc file is something like this

    [html]<p>Ime: {%ime%} Email: {%email%}[/html]

    now from database i get variables to populate my inc file. my $ime array with names lookes like this for now

    PHP Code:
    $podaci['ime'] = array ("duje""pave""mate""sime"); 
    print_red it lookes like this

    array ([ime] =>
    [0] -> 'duje'
    [1] -> 'pave')
    etc...

    but then i use pregmatch in (for now) eg. foreach loop to replace every token (%ime% and %email%) with user info, to get something like

    [html]<p>Ime: Duje E-Mail: duje@duje</p>
    <p>Ime: Pave E-Mail: pave@pave</p>[/html]
    etc, in that order

    my pregmatch is this

    PHP Code:
    foreach ($podaci['ime'] as $key => $val) {
    preg_replace ('/{%ime%}/'$val$html_inc_file);

    it tourns out to be a quite dificult to work with array that has structure like that, and i would also like not to use array_keys or array_values so that it goes as fast as possible..

    hope you understand. i would give you more info, but i'm on remote computer and i don't have direct access to scrip, but tomorow i could write more data

  • #6
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by Stooshie View Post
    "extract()" just turns an array into a set of vars. (eg $myarray["mykey"] = "myvalue" just becomes $mykey = "myvalue").
    yes i know, but it looks like i don't know how to use it right. maybe i didn't put to much effort couse i have sooo much other stuff i need to tend to :busybusy:


  •  

    Posting Permissions

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