Julian Posted January 12, 2007 Share Posted January 12, 2007 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 More sharing options...
Barand Posted January 12, 2007 Share Posted January 12, 2007 [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 More sharing options...
Julian Posted January 12, 2007 Author Share Posted January 12, 2007 Awesome, BarandWorked great!!! Link to comment https://forums.phpfreaks.com/topic/33952-help-with-array/#findComment-159450 Share on other sites More sharing options...
Julian Posted January 12, 2007 Author Share Posted January 12, 2007 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 More sharing options...
Barand Posted January 12, 2007 Share Posted January 12, 2007 [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 More sharing options...
Julian Posted January 12, 2007 Author Share Posted January 12, 2007 Thanks for the response.For example:I have 20 diferent rows showing right nowI 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 whileif ($i < 3) echo "</tr>";echo "</table>"; } Link to comment https://forums.phpfreaks.com/topic/33952-help-with-array/#findComment-159585 Share on other sites More sharing options...
Barand Posted January 12, 2007 Share Posted January 12, 2007 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 rowif ($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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.