juggernaut Posted December 6, 2006 Share Posted December 6, 2006 Hi, normally I'd just search for answers but all I've found won't help me...Dumb this down for me if you can as I'm kinda new to loops or creating them properly.I want to create this array from a set of rows in a mysql table and show the array as html elements.The rows include: $name, $url. The context I want to output is: <a href="$url">$name</a><br>How do I call these variables and place them into this script? I've tried various methods and the result is the word "Array" over and over. I left the basic script to allow someone to easily modify it.How do I get $items to show the above context? any help will be appreciated and a small reward offered for any help!The script basically creates a set of evenly distributed columns based on the data in the array:[code]<?php//Array$items = array('1','2','3','4','5','6','7','8'); // Default # of Columns$defcolumns = 4;// Number of columns$numcols = isset($_GET['columns']) && $_GET['columns']>0 ? $_GET['columns'] : $defcolumns;// Number of columns + 1$columnsplusone = $numcols + 1;// Number of Items$numitems = count($items);// Number of Rows$numrows = ceil($numitems/$numcols); ?><br><table border="0" width="500"><th colspan="100%">Array Title</th><?php for ($row=1; $row <= $numrows; $row++) { // Initialize Cells $cell = 0;?> <tr><?php for ($col=1; $col <= $numcols; $col++) {?> <td width="25%" align="center"><?php if ($col===1) { $cell += $row; print $items[$cell - 1]; } else { $cell += $numrows; print $items[$cell - 1]; }?> </td><?php }?> </tr><?php }?></table>[/code]This is my first post, sorry if I've screwed something up! I read the rules. Link to comment https://forums.phpfreaks.com/topic/29718-need-help-with-creating-an-array-from-sql-query/ Share on other sites More sharing options...
Psycho Posted December 6, 2006 Share Posted December 6, 2006 If the data is from a database query, there are better ways of displaying the data without needing to dump it into an array.In any event you will need to show us the query you are running or the format of the array you have created. Link to comment https://forums.phpfreaks.com/topic/29718-need-help-with-creating-an-array-from-sql-query/#findComment-136420 Share on other sites More sharing options...
juggernaut Posted December 6, 2006 Author Share Posted December 6, 2006 Hey mjdamato.I'd appreciate a link to a better way of doing what I'm asking... which is 4 columns of data evenly distributed depending on the amount of links in my database.Here's the query I want to run.[code]<?phpimport_request_variables("gP", "val_");$user_db = 'username';$pass_db = 'password';$host_db = 'localhost';$db = 'database';@mysql_connect ($host_db, $user_db, $pass_db);@mysql_select_db ($db);$table = 'tablename';$result = mysql_query("SELECT * FROM $table WHERE id NOT IN ($val_sid) AND active = '1'");?>[/code]The array looks like this:[code]$items = array('1','2','3','4','5','6','7','8'); [/code]I want it to look like this based on the query results:[code]$items = array('<a href="$url">$name</a><br>','<a href="$url">$name</a><br>','<a href="$url">$name</a><br>','<a href="$url">$name</a><br>'); [/code]Obviously ordered by the query or what I put in the query later on. I'm quite sure this is going to look very messy to someone with more experience, but this is the best I could come up with... Any links to tutorials to similar things would be appreciated. Link to comment https://forums.phpfreaks.com/topic/29718-need-help-with-creating-an-array-from-sql-query/#findComment-136433 Share on other sites More sharing options...
Psycho Posted December 6, 2006 Share Posted December 6, 2006 The only part I'm unlear about is "4 columns of data evenly distributed". Do you mean you want the data displayed like this[code]Link 1 | Link 6 | Link11 | Link16Link 2 | Link 7 | Link12 | Link17Link 3 | Link 8 | Link13 | Link18Link 4 | Link 9 | Link14 | Link19Link 5 | Link 10 | Link15 | Link20[/code] Link to comment https://forums.phpfreaks.com/topic/29718-need-help-with-creating-an-array-from-sql-query/#findComment-136460 Share on other sites More sharing options...
juggernaut Posted December 6, 2006 Author Share Posted December 6, 2006 Yes sir.So if there were 21 links, the order would give 6 links to the first 3 columns and only 3 in the last etc.Which is what that example does which I posted... only I tried without luck to show data from my database. Link to comment https://forums.phpfreaks.com/topic/29718-need-help-with-creating-an-array-from-sql-query/#findComment-136468 Share on other sites More sharing options...
Psycho Posted December 6, 2006 Share Posted December 6, 2006 How 'bout this:[code]<?php$columns = 4;set_include_path('./includes');require_once ("connectDB.php");$query = "SELECT * FROM genres ORDER BY displayname";$result = mysql_query($query) or die ("Query:<br>".$query."<br>Error:<br>".mysql_error());$recCount = mysql_num_rows($result);if ($recCount==0) { echo "There were no results.";} else { echo "<table><tr>"; $currentRec = 0; for ($col=1; $col<=$columns; $col++) { echo "<td valign=\"top\">"; for ($i=0; $i<($recCount/$columns); $i++) { if ($currentRec<$recCount) { $record = mysql_fetch_assoc($result); echo "<a href=\"".$record['url']."\">".$record['name']."</a><br>"; $currentRec++; } } echo "</td>"; } echo "</tr></table>";}?>,/code][/code] Link to comment https://forums.phpfreaks.com/topic/29718-need-help-with-creating-an-array-from-sql-query/#findComment-136531 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.