TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Forum help (http://www.talkphp.com/absolute-beginners/1757-forum-help.html)

Tanax 12-17-2007 04:35 PM

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??

Jay 12-17-2007 05:06 PM

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

PHP Code:

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



Tanax 12-17-2007 05:19 PM

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..

hostfreak 12-17-2007 05:43 PM

Quote:

Originally Posted by Tanax (Post 6765)
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.

Tanax 12-17-2007 06:02 PM

Quote:

Originally Posted by hostfreak (Post 6767)
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

xenon 12-17-2007 06:10 PM

Quote:

Originally Posted by Tanax (Post 6769)
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...

hostfreak 12-17-2007 06:13 PM

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
        )

)


Nor 12-17-2007 06:24 PM

Quote:

Originally Posted by Jay (Post 6764)
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...

Tanax 12-17-2007 06:25 PM

@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.... 


hostfreak 12-17-2007 06:30 PM

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.

Tanax 12-17-2007 06:50 PM

Quote:

Originally Posted by hostfreak (Post 6776)
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); 

??

Jay 12-17-2007 07:00 PM

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.

Tanax 12-17-2007 07:15 PM

Because it's used within a function in a class, and it's not complete, so.. :P


All times are GMT. The time now is 06:18 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0