wfcentral Posted October 25, 2008 Share Posted October 25, 2008 I will do my best to explain this. I am creating a page to show "sponsors" for an event. This page is maintained by a database page (already built). Now, I need to display the info from the database onto the page and I'm not sure how to do what I need. tbl_categories (levels of sponsorship like Bronze, Silver, Gold) tbl_sponsors (companies / people who sponsored) the idea is that someone will maintain this and I gave them the ability to add/edit categories (levels of sponsorship) - since the naming may change I added a field called "rank" so they can put numbers in there to determine how it shows up on the page. Lower numbers represent closer to top of page (higher sponsor) while bigger numbers are lower down page (less sponsor). Some categories have only two graphics and look best in 2 columns while other categories have lots of text names and look best in 3 or 4 columns. So, I also gave the tbl_categories a field called "columns" I am new to hand coding php and not sure if I need arrays, sets, or what to do the following... get all categories from database and sort by "rank" start with 1st group and display all sponsors in that set (dividing by number of columns set in field "columns" go to next category in ranking and display all sponsors in that set (again divide columns according to field "columns" go to next category in ranking... etc Link to comment https://forums.phpfreaks.com/topic/130029-how-to-pull-sets-of-information-into-multi-columns/ Share on other sites More sharing options...
FForce2195 Posted October 25, 2008 Share Posted October 25, 2008 You need to grab a book that explains PHP and MySQL basics. Link to comment https://forums.phpfreaks.com/topic/130029-how-to-pull-sets-of-information-into-multi-columns/#findComment-674238 Share on other sites More sharing options...
wfcentral Posted October 25, 2008 Author Share Posted October 25, 2008 Ouch... I have already looked in online books and was ready to buy one, but I did not see an answer to what I am looking for. I don't need a book on PHP / MySQL "basics" as I have already created the page that accesses the database, sorts the items, and displays them in a table on my page (basic stuff). What I'm asking for is... - what piece of code would you use to break out of a while loop when one field changes (sponsor level) for example... OR - should I be pulling out the "sets" first and then running a series of WHILE loops to go through each set. All I'm asking for is a little help in "how would you do this" and then I will go look it up and read on it... Link to comment https://forums.phpfreaks.com/topic/130029-how-to-pull-sets-of-information-into-multi-columns/#findComment-674369 Share on other sites More sharing options...
dropfaith Posted October 25, 2008 Share Posted October 25, 2008 http://us2.php.net/break just use a break? Link to comment https://forums.phpfreaks.com/topic/130029-how-to-pull-sets-of-information-into-multi-columns/#findComment-674372 Share on other sites More sharing options...
wfcentral Posted October 25, 2008 Author Share Posted October 25, 2008 thank you! that does look like the right path, I would only need to change this line... $arr = array('one', 'two', 'three', 'four', 'stop', 'five'); In my scenario, the user will enter a number in the "level" field to sort these sponsor groups. So, if they want "Golden Hero Level" to show up first they will put "10" in the level field... if they want "Super Star Level" to show up next they may put in "11" or "20" (I always tell them to put in units of 10 so if they want to create a new level later and have it show up between two other levels there is "space") So, for that reason I will need to fill this array with the values from field "level" sorted ASC I will start looking for a way to do that now - thanks Link to comment https://forums.phpfreaks.com/topic/130029-how-to-pull-sets-of-information-into-multi-columns/#findComment-674378 Share on other sites More sharing options...
Barand Posted October 25, 2008 Share Posted October 25, 2008 this function will put an array of data into N columns, eg A D G B E H C F function array2cols ($arr, $cols) { sort ($arr); $k = count($arr); $rows = ceil($k/$cols); $overflow = $k % $cols; echo "<table>\n"; for ($r=0; $r<$rows; $r++) { echo "<tr>\n"; for ($c=0; $c<$cols; $c++) { $key = $r + $c * $rows ; if ($overflow && $c >= $overflow) { $key = $r + $c * ($rows-1) + $overflow; if ($r==$rows-1) $key = ''; } $item = isset($arr[$key]) ? $arr[$key] : ' '; echo "<td style='padding: 0 20'>$item</td>"; } echo "</tr>\n"; } echo "</table>\n"; } so if your data is something like this [pre] category sponsor ========= ========= catID ----+ sponsorID level | company rank +---< catID columns [/pre] then $sql = "SELECT c.level, c.columns, c.rank, GROUP_CONCAT(s.company) as companies FROM wfcategory c INNER JOIN wfsponsor s ON c.catid = s.catid GROUP BY c.rank ORDER BY c.rank"; $res = mysql_query($sql) or die(mysql_error()); while (list($level,$cols,$rank,$comps)=mysql_fetch_row($res)) { echo "<h3>$level</h3>"; array2cols(explode(',', $comps), $cols); } Link to comment https://forums.phpfreaks.com/topic/130029-how-to-pull-sets-of-information-into-multi-columns/#findComment-674402 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.