axm Posted September 22, 2008 Share Posted September 22, 2008 Hey all, bear with me whilst i try to explain this! I have two <ul> lists that are to be populated with data from a MySQL database. The <ul>'s sit side-by-side. I wish to create a script that will build these so that they are both equal (or one has 1 fewer if the total is an odd number). So I was thinking of using a COUNT to count the total of entries in the table. Dividing that value in half and building the two <ul>'s. However, I'm not sure how to do it! Many thanks in advance :] Quote Link to comment https://forums.phpfreaks.com/topic/125295-solved-spanning-two-uls/ Share on other sites More sharing options...
Adam Posted September 22, 2008 Share Posted September 22, 2008 Could do something like.. <?php // assuming you've connected and made the query $col = 1; while ($record = mysql_fetch_array($yourQuery)) { if ($col == 1) { $col1[] = $record['someField']; $col = 2; } else { $col2[] = $record['someField']; $col = 1; } } print '<ul>'; foreach ($col1 as $data) { print '<li>' .$data. '</li>'; } print '</ul>'; print '<ul>'; foreach ($col2 as $data) { print '<li>' .$data. '</li>'; } print '</ul>'; ?> Could be a simpler way.. just a quick idea.. Adam Quote Link to comment https://forums.phpfreaks.com/topic/125295-solved-spanning-two-uls/#findComment-647670 Share on other sites More sharing options...
axm Posted September 22, 2008 Author Share Posted September 22, 2008 Cheers MrAdam! Though I don't understand how it counts the number of fields then divides them in two? Do you mind briefly explaining the code? Many thanks :] Quote Link to comment https://forums.phpfreaks.com/topic/125295-solved-spanning-two-uls/#findComment-647681 Share on other sites More sharing options...
Adam Posted September 22, 2008 Share Posted September 22, 2008 Oh wait sorry, fields? So for each record in db table, you want to split the field into two and put half in one list, and half in the other list? Or do you mean like two records at the side of each other.. like: record1 record2 record3 record4 etc.. ? Adam Quote Link to comment https://forums.phpfreaks.com/topic/125295-solved-spanning-two-uls/#findComment-647682 Share on other sites More sharing options...
axm Posted September 22, 2008 Author Share Posted September 22, 2008 Sorry for my poor explanation! I have a set of records i.e. ID title text ID title text ID title text ID title text etc... I want to total these and split the total in half so they display in two columns :] Hope that's better! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/125295-solved-spanning-two-uls/#findComment-647690 Share on other sites More sharing options...
Adam Posted September 22, 2008 Share Posted September 22, 2008 Ah right well, the method I showed you before will basically take one record at a time and you can save whatever fields you like into an array that alternates each time.. effictely being like.. "one for you and one for me" between the two arrays. So record 1 is in array 1, record 2 is in array 2, record 3 is in array 1, record 4 is in array 2, etc. So basically splitting the records into lists, or columns if you were to display the lists at the side of each other. you don't need to count them up, that would sort of be a long way round. All you then do is loop thru each array and print out the contents in the array... Adam Quote Link to comment https://forums.phpfreaks.com/topic/125295-solved-spanning-two-uls/#findComment-647697 Share on other sites More sharing options...
Adam Posted September 22, 2008 Share Posted September 22, 2008 .... oh wait. Do you want them in format like: record 1 .. record 2 record 3 .. record 4 or.. record 1 .. record 3 record 2 .. record 4 ?? Quote Link to comment https://forums.phpfreaks.com/topic/125295-solved-spanning-two-uls/#findComment-647699 Share on other sites More sharing options...
axm Posted September 22, 2008 Author Share Posted September 22, 2008 Oh I think I'm beginning to understand! I don't believe it matters really in what order they go however, I would prefer them to go record 1 record 3 record 2 record 4 Cheers MrAdam! :] Quote Link to comment https://forums.phpfreaks.com/topic/125295-solved-spanning-two-uls/#findComment-647702 Share on other sites More sharing options...
Adam Posted September 22, 2008 Share Posted September 22, 2008 ahh okay, could use this then: <?php // assuming you've connected and made the query $halfWay = ceil(mysql_num_rows($yourQuery) / 2); $x = 0; while ($record = mysql_fetch_array($yourQuery)) { if ($x < $halfWay) { $col1[] = $record['id'] .' '. $record['title'] .' '. $record['text']; } else { $col2[] = $record['id'] .' '. $record['title'] .' '. $record['text']; } $x++; } print '<ul>'; foreach ($col1 as $data) { print '<li>' .$data. '</li>'; } print '</ul>'; print '<ul>'; foreach ($col2 as $data) { print '<li>' .$data. '</li>'; } print '</ul>'; ?> .. not tested but should give you the desired result... Quote Link to comment https://forums.phpfreaks.com/topic/125295-solved-spanning-two-uls/#findComment-647710 Share on other sites More sharing options...
axm Posted September 22, 2008 Author Share Posted September 22, 2008 Many thanks MrAdam! I shall try this and let you know how I get on. $20 donation coming PHPFreaks' way :] Quote Link to comment https://forums.phpfreaks.com/topic/125295-solved-spanning-two-uls/#findComment-647714 Share on other sites More sharing options...
axm Posted September 22, 2008 Author Share Posted September 22, 2008 Right... it's all working okay except i forgot to mention that the data in the <li>'s are links... here is the code I have done (you can easily see that I have used && to demonstrate what I wish to do haha!). <div id="testimonials_left"> <?php print '<ul>'; foreach ($col1 as $data && $col1ID as $id) { print '<li><a title="AAR ' . $id . ' testimonial" href="read_testimonial.php?id=' . $id . '">' .$data. '</a></li>'; } print '</ul>'; ?> </div> <div id="testimonials_right"> <?php print '<ul>'; foreach ($col2 as $data && $col2ID as $id) { print '<li><a title="AAR ' . $id . ' testimonial" href="read_testimonial.php?id=' . $id . '">' .$data. '</a></li>'; } print '</ul>'; ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/125295-solved-spanning-two-uls/#findComment-647726 Share on other sites More sharing options...
axm Posted September 22, 2008 Author Share Posted September 22, 2008 Right figured out how to do it! Here is the code: <?php include('includes/connect.php'); $yourQuery = mysql_query("SELECT * FROM testimonials", $constr); $halfWay = ceil(mysql_num_rows($yourQuery) / 2); $x = 0; while ($record = mysql_fetch_array($yourQuery)) { if ($x < $halfWay) { $col1[] = $record; } else { $col2[] = $record; } $x++; } ?> <div id="testimonials_left"> <?php print '<ul>'; foreach ($col1 as $data) { print '<li><a title="AAR ' . $data['id'] . ' testimonial" href="read_testimonial.php?id=' . $data['id'] . '">' .$data['title']. '</a></li>'; } print '</ul>'; ?> </div> <div id="testimonials_right"> <?php print '<ul>'; foreach ($col2 as $data) { print '<li><a title="AAR ' . $data['id'] . ' testimonial" href="read_testimonial.php?id=' . $data['id'] . '">' .$data['title']. '</a></li>'; } print '</ul>'; ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/125295-solved-spanning-two-uls/#findComment-647761 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.