TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-17-2007, 04:35 PM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default Forum help

Heya, you might have seen my thread ..

Anyways, I'm just wondering if this is OKAY:
php Code:
$i = 1;
               
                while($thread = mysql_fetch_array($query)) {
                   
                    $this->threads_details[$i] = $thread;
                   
                    $i++;
                   
                }

I want the thread_details array to be like this:

Code:
thread_details[1] => array('thread_id' = 43, 'thread_posts' = 25, 'thread_views' = 656, 'thread_author' = 'Tanax')
thread_details[2] => array('thread_id' = 25, 'thread_posts' = 78, 'thread_views' = 1656, 'thread_author' = 'Wildhoney')
Get the point??
Tanax is offline  
Reply With Quote
Old 12-17-2007, 05:06 PM   #2 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 60
Thanks: 5
Jay is on a distinguished road
Default

You don't need to manually iterate over the array, just use []

PHP Code:
while( $thread mysql_fetch_array$query ) )
{
    
$this->threads_details[] = $thread;

Jay is offline  
Reply With Quote
Old 12-17-2007, 05:19 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

But then every thread id and etc, will be on the same array..index or w.e it's called :S

I want to seperate them, so it's

[1] => information about first thread that's found. Thread id, thread author etc..
[2] => information about second thread that's found..
[3] => -||-

etc..
Tanax is offline  
Reply With Quote
Old 12-17-2007, 05:43 PM   #4 (permalink)
The Wanderer
 
hostfreak's Avatar
 
Join Date: Oct 2007
Posts: 21
Thanks: 1
hostfreak is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
But then every thread id and etc, will be on the same array..index or w.e it's called :S
Tanax, that is incorrect. From the php manual:

PHP Code:
<?php
$arr 
= array(=> 112 => 2);

$arr[] = 56;    // This is the same as $arr[13] = 56;
                // at this point of the script

$arr["x"] = 42// This adds a new element to
                // the array with key "x"
                
unset($arr[5]); // This removes the element from the array

unset($arr);    // This deletes the whole array
?>
As you can see, with the square-bracket syntax, each key value will be different; it will be incremented one above the previous value.
hostfreak is offline  
Reply With Quote
Old 12-17-2007, 06:02 PM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by hostfreak View Post
Tanax, that is incorrect. From the php manual:

PHP Code:
<?php
$arr 
= array(=> 112 => 2);

$arr[] = 56;    // This is the same as $arr[13] = 56;
                // at this point of the script

$arr["x"] = 42// This adds a new element to
                // the array with key "x"
                
unset($arr[5]); // This removes the element from the array

unset($arr);    // This deletes the whole array
?>
As you can see, with the square-bracket syntax, each key value will be different; it will be incremented one above the previous value.
Yea, but I need

$array[1] = array('thread_id' ..... );
$array[2] = array('thread_id' ..... );
$array[3] = .. etc
Tanax is offline  
Reply With Quote
Old 12-17-2007, 06:10 PM   #6 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Yea, but I need

$array[1] = array('thread_id' ..... );
$array[2] = array('thread_id' ..... );
$array[3] = .. etc
But why? Do you intend to do some special operations on the
indexes? I thought you just need the values, not the indexes... Basically, the code you wrote is okay, but you don't need to keep an additional count of the array..it's useless...if you need, do something like:

Code:
$this->threads_details = array( 0 => null ); // this should go into the constructor, perhaps

while( ... ) { /* do your stuff */ }
And nothing will be the same, each array child of the $this->threads_details will represent a unique row from the database...
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 12-17-2007, 06:13 PM   #7 (permalink)
The Wanderer
 
hostfreak's Avatar
 
Join Date: Oct 2007
Posts: 21
Thanks: 1
hostfreak is on a distinguished road
Default

Are you saying you want an individual array for each one? If so, I'm not really sure why?... Something like the following should be efficient enough:

PHP Code:
<?php

    $arr
[] = array('thread_id' => '43''thread_posts' => '25''thread_views' => '656''thread_author' => 'Tanax',);
    
$arr[] = array('thread_id' => '25''thread_posts' => '78''thread_views' => '1656''thread_author' => 'Wildhoney',);
?>
Output:

Code:
Array
(
    [0] => Array
        (
            [thread_id] => 43
            [thread_posts] => 25
            [thread_views] => 656
            [thread_author] => Tanax
        )

    [1] => Array
        (
            [thread_id] => 25
            [thread_posts] => 78
            [thread_views] => 1656
            [thread_author] => Wildhoney
        )

)
hostfreak is offline  
Reply With Quote
The Following User Says Thank You to hostfreak For This Useful Post:
Tanax (12-17-2007)
Old 12-17-2007, 06:24 PM   #8 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Quote:
Originally Posted by Jay View Post
You don't need to manually iterate over the array, just use []

PHP Code:
while( $thread mysql_fetch_array$query ) )
{
    
$this->threads_details[] = $thread;

This post sums it up...
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 12-17-2007, 06:25 PM   #9 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

@hostfreak- That's exactly what I want!

Because I'm fetching the threads in a specific forum, and I want all threads in the same array.

If I fetch those threads, all threads will have different IDs, and that's why I want to seperate them with different arrays, but still in the same variable.. if that makes any sense.

Because there will be multiple 'thread_id' keys if I put them in the same.
And it'll be a total mess. That's why I want to seperate them :)

But when I add them, should it be like:

PHP Code:
$array[$i] = mysql_fetch_array($query); 
or

PHP Code:
$array[$i][] = mysql_fetch_array.... 
Tanax is offline  
Reply With Quote
Old 12-17-2007, 06:30 PM   #10 (permalink)
The Wanderer
 
hostfreak's Avatar
 
Join Date: Oct 2007
Posts: 21
Thanks: 1
hostfreak is on a distinguished road
Default

I understand why you want to do that. At first I thought you meant a completely different array for each one, my mistake. What I posted is known as a multidimensional array.
hostfreak is offline  
Reply With Quote
Old 12-17-2007, 06:50 PM   #11 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by hostfreak View Post
I understand why you want to do that. At first I thought you meant a completely different array for each one, my mistake. What I posted is known as a multidimensional array.
Ah, so it should be like:

PHP Code:
$arr[] = mysql_fetch_array($query); 
??
Tanax is offline  
Reply With Quote
Old 12-17-2007, 07:00 PM   #12 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 60
Thanks: 5
Jay is on a distinguished road
Default

Tanax, try it? I don't see why you don't just go through a rigorous series of trial & error..

Try the mentioned methods or your methods, then use var_dump or print_r to see the output, decide for yourself if it's what you need.
Jay is offline  
Reply With Quote
Old 12-17-2007, 07:15 PM   #13 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Because it's used within a function in a class, and it's not complete, so.. :P
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:12 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design