Jump to content

Help with Array!


Julian

Recommended Posts

Hi guys!

I have this array:

  $new_type_array = array (
        1 => 'Volcanoes, Rainforest and Hot Springs Adventures',
        2 => 'City Escapes, Jungle Treks and River Rafting Adventures',
        3 => 'Combination Adventures',
        4 => 'Beach Adventures',
        );

I manually typed the array, but the info comes from a table in database `packages`.

Example:
      array(
      id_packages => 'name'  (Info from the database));

What i'm trying to do is create this array with the info from the table.  Is that possible?  Create a variable that brings all the info from the table `packages` and put it on the array with this syntax:  1 => 'Mountain'

Thanks guys you're the best.
Link to comment
https://forums.phpfreaks.com/topic/33952-help-with-array/
Share on other sites

[code]
<?php   
$sql = "SELECT id, name FROM packages ORDER BY id";
$res = mysql_query($sql) or die(mysql_error());

$new_type_array = array();
while (list($id, $name) = mysql_fetch_row($res)) {
    $new_type_array[$id] = $name;
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/33952-help-with-array/#findComment-159440
Share on other sites

I found a small problem!  Sorry for bother you guys again.  But I'm so close to finish this, that I can almost taste it... :-)

Here's the code:
[code]
<?php 
  $dbtypes = $row_tour['id_tour'];
  $db_array = explode(',', $dbtypes);
 
  $sql = "SELECT id_tour, tour FROM tours ORDER BY id_tour";
$res = mysql_query($sql) or die(mysql_error());

$new_type_array = array();
while (list($id_tour, $tour) = mysql_fetch_row($res)) {
    $new_type_array[$id_tour] = $tour;
}
       
foreach ($new_type_array as $typeid =>$desc) {
echo "<ul><li>$desc</li></ul>";
}
 
?>
[/code]

I just want to show the results on even columns, because now I get a very large drop down list.  Thanks again.

Regards
Link to comment
https://forums.phpfreaks.com/topic/33952-help-with-array/#findComment-159570
Share on other sites

[quote author=Julian link=topic=122162.msg503604#msg503604 date=1168643244]
I just want to show the results on even columns, because now I get a very large drop down list.  Thanks again.
[/quote]

Can you explain what you mean by that?
Link to comment
https://forums.phpfreaks.com/topic/33952-help-with-array/#findComment-159579
Share on other sites

Thanks for the response.

For example:
I have 20 diferent rows showing right now

I just want to put the result rows on 2 columns showing 10 and 10 on each.  In order to keep my page design.  Tried this script but I don't get results:

$dbtypes = $row_tour['id_tour'];
  $db_array = explode(',', $dbtypes);
 
  $sql = "SELECT id_tour, tour FROM tours ORDER BY id_tour";
$res = mysql_query($sql) or die(mysql_error());

$new_type_array = array();
while (list($id_tour, $tour) = mysql_fetch_row($res)) {
    $new_type_array[$id_tour] = $tour;
}
       
foreach ($new_type_array as $typeid =>$desc) {
echo "<table>";
$i = 0;
while ($rec = mysql_fetch_assoc($res)) {
  //$value = $desc;
  $i++;
  switch ($i) {
      case 1 : echo "<tr><td>$desc</td>";
              break;
      case 2 : echo "<td>$desc</td>";
              break;
      case 3 : echo "<td>$desc</td></tr>";
              $i=0;
              break;
  } // End switch
} // End while
if ($i < 3)
    echo "</tr>";
echo "</table>";
}
Link to comment
https://forums.phpfreaks.com/topic/33952-help-with-array/#findComment-159585
Share on other sites

Unless you need the array elsewhere, just output as you read the query results

[code]
<?php   
$sql = "SELECT id, name FROM packages ORDER BY id";
$res = mysql_query($sql) or die(mysql_error());

echo '<table>';
$count = 0;
while (list($id, $name) = mysql_fetch_row($res)) {
    if ($count % 2 == 0) echo '<tr>';
    echo "<td>$name</td>";
    $count++;
    if ($count % 2 == 0) echo '</tr>';
}
// if we got to the end but haven't closed the row
if ($count % 2 != 0) echo '</tr>';
echo '</table>';
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/33952-help-with-array/#findComment-159590
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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