multidimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

multidimensional array

Post by hairyjim »

How do I get several records in a MySQL db into a multidimensional array, I've been Googling but I have not found anything which has been particularly useful in my understanding.

I want Record 'x' (1,2,3 etc) as the top, then I want the pertinent details of each record in the sub array.

-Record 1
--Title
--Descritpion
--Image
--Date
-Record 2
--Title
--Descritpion
--Image
--Date

...and so on

Links, comments as always appreciated.
SvanteH
Forum Commoner
Posts: 50
Joined: Wed Jul 08, 2009 12:25 am

Re: multidimensional array

Post by SvanteH »

SELECT * FROM `tbl_Records`;

Code: Select all

while($row = mysql_fetch_array($result))
{
   echo "Record: ". $row["id"]."<br>";
   echo $row["desc"]."<br>";  
}
and so on
Last edited by SvanteH on Thu Jul 09, 2009 3:29 am, edited 1 time in total.
ctmaster
Forum Newbie
Posts: 7
Joined: Wed Jul 08, 2009 10:45 pm

Re: multidimensional array

Post by ctmaster »

Do you want just an array after select the database?

If you know PEAR mdb2 package, you can use it.

You can use it like below:

$results = $conn->queryAll("select * from example_table", array(), MDB2_FETCHMODE_ASSOC);

then you can get the array.
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Re: multidimensional array

Post by hairyjim »

SvanteH wrote:SELECT * FROM `tbl_Records`;

Code: Select all

while($row = mysql_fetch_array($result))
{
   echo "Record: ". $row["id"]."<br>";
   echo $row["desc"]."<br>";  
}
and so on
I think this only gives me a 1D array, I essentially want a 2D array, with the Record ID at the top level and also being an array in itself containing the records pertinent details.

What I am trying to achieve is that I want to populate each section of the below 'featuresd slider' code with the latest four articles in the DB. I wanted a 2D array so I could just easily reference the record ID and its associated sub data easily in the below code without having to echo out pieces of HTML from the PHP and using a LOOP and an IF statement to id the first record whose HTML is slightly different. I like to try and keep my HTML outside of the PHP code. Am I completly barking up the worng tree here and missing something really obvious?

Code: Select all

 
<!-- BEGIN feature -->    
    <div id="featured" >
      <ul class="ui-tabs-nav">
        <li class="ui-tabs-nav-item ui-tabs-selected" id="nav-fragment-1"><a href="#fragment-1"><img src="<?php $RecArray[0][Small-Image] ?>" alt="" /><span><?php $RecArray[0][Title] ?></span></a></li>
        <li class="ui-tabs-nav-item" id="nav-fragment-2"><a href="#fragment-2"><img src="<?php $RecArray[1][Small-Image] ?>" alt="" /><span><?php $RecArray[1][Title] ?></span></a></li>
        <li class="ui-tabs-nav-item" id="nav-fragment-3"><a href="#fragment-3"><img src="<?php $RecArray[2][Image] ?>" alt="" /><span><?php $RecArray[2][Title] ?></span></a></li>
        <li class="ui-tabs-nav-item" id="nav-fragment-4"><a href="#fragment-4"><img src="<?php $RecArray[3][Image] ?>" alt="" /><span><?php $RecArray[3][Title] ?></span></a></li>
      </ul>
      <!-- First Content -->
      <div id="fragment-1" class="ui-tabs-panel" style=""> <img src="<?php $RecArray[0][Large-Image] ?>" alt="" />
        <div class="info" >
          <h2><a href="#" ><?php $RecArray[0][Title] ?></a></h2>
          <p><?php $RecArray[0][Description] ?>...<a href="#" >read more</a></p>
        </div>
      </div>
      <!-- Second Content -->
      <div id="fragment-2" class="ui-tabs-panel ui-tabs-hide" style=""> <img src="<?php $RecArray[1][Large-Image] ?>" alt="" />
        <div class="info" >
          <h2><a href="#" ><?php $RecArray[1][Title] ?></a></h2>
          <p><?php $RecArray[1][Description] ?>...<a href="#" >read more</a></p>
        </div>
      </div>
      <!-- Third Content -->
      <div id="fragment-3" class="ui-tabs-panel ui-tabs-hide" style=""> <img src="<?php $RecArray[2][Large-Image] ?>" alt="" />
        <div class="info" >
          <h2><a href="#" ><?php $RecArray[2][Title] ?></a></h2>
          <p><?php $RecArray[2][Description] ?>...<a href="#" >read more</a></p>
        </div>
      </div>
      <!-- Fourth Content -->
      <div id="fragment-4" class="ui-tabs-panel ui-tabs-hide" style=""> <img src="<?php $RecArray[3][Large-Image] ?>" alt="" />
        <div class="info" >
          <h2><a href="#" ><?php $RecArray[3][Title] ?></a></h2>
          <p><?php $RecArray[3][Description] ?>...<a href="#" >read more</a></p>
        </div>
      </div>
    </div>
    <!-- END feature -->
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: multidimensional array

Post by VladSun »

... a 2D array, with the Record ID at the top level and also being an array in itself containing the records pertinent details...
You can explain it in a perfect way and still can't construct the PHP code from your requirements?!? :lol:
Please, follow your explanation (the one above) and try to implement it.
If you still need any help (I frankly doubt you'll) post your code.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: multidimensional array

Post by pickle »

pseudo-code:

Code: Select all

$resultSet = mysql_do_query($query);
foreach($Row in $resultSet)
{
  $myMultiArray[$Row['id_column']] = array(
    'title'=>$Row['title_column'],
    'description'=>$Row['description_column'],
    'image'=>$Row['image_column'],
    'date'=>$Row['date_column']
  );
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply