Page 1 of 1

multidimensional array

Posted: Thu Jul 09, 2009 3:05 am
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.

Re: multidimensional array

Posted: Thu Jul 09, 2009 3:08 am
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

Re: multidimensional array

Posted: Thu Jul 09, 2009 3:23 am
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.

Re: multidimensional array

Posted: Thu Jul 09, 2009 4:02 am
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 -->
 

Re: multidimensional array

Posted: Thu Jul 09, 2009 4:24 am
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.

Re: multidimensional array

Posted: Thu Jul 09, 2009 10:17 am
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']
  );
}