Jump to content

[SOLVED] Spanning two ULs


axm

Recommended Posts

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 :]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

 

 

Link to comment
Share on other sites

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>

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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