Jump to content

Recommended Posts

Hello,

 

I am just trying to return the table below in the following format but I am having some trouble in getting the syntax right. Still a php novice!

 

mysql table (id colomn hidden):

 

portfolioItems  category

104                  4

103                  48

100                  7

100                  5

105                  5

105                  4

105                  18

104                  7

105                  48

105                  7

104                  5

103                  7

100                  4

100                  18

 

 

The format I would like to return is:

 

Array(

          [portfolioItem01] = Array(

                                                    [category01]

                                                    [category02]

                                                    [category03]

                                                    [category04]

                                                    [etc]

                                                                          )

          [portfolioItem02] = Array(

                                                    [category01]

                                                    [category02]

                                                    [category03]

                                                    [category04]

                                                    [etc]

                                                                          )

          [portfolioItem03] = Array(

                                                    [category01]

                                                    [category02]

                                                    [category03]

                                                    [category04]

                                                    [etc]

                                                                          )

                                                                            )

 

This is the code I am using currently but it's not working,

 

 


$query = "SELECT category, portfolioItem FROM PIC ORDER BY portfolioItem, category DESC"; 
$result = mysql_query($query) or die(mysql_error());


$catResultArray[] = array();
$currentPItem = -1;
// Print out result
while($row = mysql_fetch_array($result)){
	if ($currentPItem!=$row['portfolioItem']){
		$currentPItem = $row['portfolioItem'];
		array_push($catResultArray, $currentPItem);
	}
	array_push($catResultArray[$currentPItem], $row['category']);
	//echo $row['portfolioItem']. " - ". $row['category'];
	//echo "<br />";
}
print_r($catResultArray);

 

....can anybody help please?

 

Thanks very much

 

Spencer

$query = "SELECT category, portfolioItem FROM PIC ORDER BY portfolioItem, category DESC";
   $result = mysql_query($query) or die(mysql_error());
   
   
   $catResultArray[] = array();
   // build array
   while($row = mysql_fetch_array($result)){
         $catResultArray[$row['portfolioItems']][] = $row['category']; // this line builds the array in the format you want
      }
      // output all items in array:
      foreach ($catResultArray as $portfolioItem => $catData)
      {
         print($portfolioItem.': <br/>'."\n"); // print portfolioItem
         foreach ($catData as $num => $category)
            print($category.'<br/>'."\n"); // print each category under portfolio
         print('<br/>'."\n");
      }
   }
   print_r($catResultArray);

Catfish I tried your code and it printed this:

 

Array

(

    [0] => Array

        (

        )

 

    [] => Array

        (

            [0] => 18

            [1] => 7

            [2] => 5

            [3] => 4

            [4] => 48

            [5] => 7

            [6] => 7

            [7] => 5

            [8] => 4

            [9] => 48

            [10] => 18

            [11] => 7

            [12] => 5

            [13] => 4

        )

 

)

 

 

Looks like it's nearly there. Ill try and work out what went wrong.

 

Thanks again.

 

Spencer

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.