 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
12-17-2007, 04:35 PM
|
#1 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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??
|
|
|
|
12-17-2007, 05:06 PM
|
#2 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Posts: 60
Thanks: 5
|
You don't need to manually iterate over the array, just use []
PHP Code:
while( $thread = mysql_fetch_array( $query ) ) { $this->threads_details[] = $thread; }
|
|
|
|
12-17-2007, 06:24 PM
|
#3 (permalink)
|
|
The Addict
Join Date: Nov 2007
Posts: 282
Thanks: 61
|
Quote:
Originally Posted by Jay
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!
|
|
|
|
12-17-2007, 05:19 PM
|
#4 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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..
|
|
|
|
12-17-2007, 05:43 PM
|
#5 (permalink)
|
|
The Wanderer
Join Date: Oct 2007
Posts: 21
Thanks: 1
|
Quote:
Originally Posted by Tanax
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(5 => 1, 12 => 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.
|
|
|
|
12-17-2007, 06:02 PM
|
#6 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by hostfreak
Tanax, that is incorrect. From the php manual:
PHP Code:
<?php
$arr = array(5 => 1, 12 => 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
|
|
|
|
12-17-2007, 06:10 PM
|
#7 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
Quote:
Originally Posted by Tanax
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.
|
|
|
|
12-17-2007, 06:13 PM
|
#8 (permalink)
|
|
The Wanderer
Join Date: Oct 2007
Posts: 21
Thanks: 1
|
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
)
)
|
|
|
|
|
The Following User Says Thank You to hostfreak For This Useful Post:
|
|
12-17-2007, 06:25 PM
|
#9 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
@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....
|
|
|
|
12-17-2007, 06:30 PM
|
#10 (permalink)
|
|
The Wanderer
Join Date: Oct 2007
Posts: 21
Thanks: 1
|
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.
|
|
|
|
12-17-2007, 06:50 PM
|
#11 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by hostfreak
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);
??
|
|
|
|
12-17-2007, 07:00 PM
|
#12 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Posts: 60
Thanks: 5
|
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.
|
|
|
|
12-17-2007, 07:15 PM
|
#13 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Because it's used within a function in a class, and it's not complete, so.. :P
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|