Split MySQL total rows between 2 HTML tables

12-28-09, 11:44 AM
|
|
Newbie Coder
|
|
Join Date: Dec 2009
Posts: 5
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
|
Split MySQL total rows between 2 HTML tables
Hi,
Needless to say I am a newb. I'm running:
Windows XP Pro 32-bit
Apache 2.2
PHP 5.3
MySQL 5.1
phpMyAdmin 3.2
I have built a simple contact table that only has three columns: p_id, name, phone. I have also designed simple PHP scripts that allow me to insert and update records. What I want to do is sort the table by the name, split the data in half, and insert both data sets into two different HTML tables. I need the two HTML table to display side by side and on the same page. Table 1 would contain records A-? and Table two would contain ?-Z. I have built a simple PHP script that inserts the data into an HTML table row by row. Having MySQL know where the half way point is and starting a new table is where I am having my problem.
I am not asking that someone write me a script, but rather point me in the right direction, recommend other similar problems with solutions, or make some kind of suggestions. I didn't know what you would call what I am trying to do so I didn't know how to search for it. Thank you in advance for any help.
Kind Regards,
Joshua
This script only displays the part of the HTML table that is looped. It also alternates the background color of each row and it omits duplicate names.
|

12-28-09, 12:49 PM
|
 |
-
|
|
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
|
|
I'd use MySQL to count the number of records returned from the query with SELECT COUNT(*) FROM `table`;, or PHP's count function (count($aArray)) - then assemble the tables.
|
|
The Following User Says Thank You to wirehopper For This Useful Post:
|
|

12-28-09, 12:53 PM
|
 |
Level II Curmudgeon
|
|
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
|
|
Quote:
Originally Posted by gigpacknaxe
What I want to do is sort the table by the name, split the data in half, and insert both data sets into two different HTML tables. I need the two HTML table to display side by side and on the same page. Table 1 would contain records A-? and Table two would contain ?-Z.
|
Do a COUNT() of all of the rows to be returned, divide by 2, and you'll have the "half-way point". Start outputing the data to the first HTML table, counting the rows as you go, and when you hit the mid-point, switch over to the second table.
Last edited by End User; 12-28-09 at 12:57 PM.
|
|
The Following User Says Thank You to End User For This Useful Post:
|
|

12-28-09, 12:55 PM
|
 |
Newbie Coder
|
|
Join Date: Oct 2006
Posts: 85
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hi Joshua,
I think you could work out how many rows you have in your table by using this function: mysql insert id, which will give you the auto incremented id of the last row added.
You could then divide that number by two (rounded up to the nearest whole number using ceil(); ).
Do this within your query of the database before echoing any data.
Then you could perhaps do a for loop to echo the format of the first table, whilst it's not reached your half way id (rounded up to the nearest whole number using ceil). Then simply do the same, starting with the half way id (+1) as the initial value and continue echoing everything from then until your last row which is obtained through your mysql_insert_id(); function (with the formatting and positioning of your second table).
I haven't used php for a while and am by no means anything more than a beginner but this seems like an option from my point of view.
Ben.
|
|
The Following User Says Thank You to j-a-m-i-n For This Useful Post:
|
|

12-28-09, 12:59 PM
|
 |
Newbie Coder
|
|
Join Date: Oct 2006
Posts: 85
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Oops. Listen to those guys. They know what they're talking about. It's better using COUNT() as that will give you the total number of rows, relevant to your query, as you may not be processing every row your table.
|

12-29-09, 02:04 PM
|
|
Newbie Coder
|
|
Join Date: Dec 2009
Posts: 5
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
First, Thank you to everyone who replyed to my question. All suggestions were good ones!!!
Ya'll recommended two different options:
COUNT()
mysql_insert_id()
I chose the explore the count method simply because as records are added and deleted the last ID might not be the actual total rows. After beating my head against the wall for quite some time, the answer did come to me. I will attach the script below and please feel free to make any suggestions you like......I am no Pro. I start by counting all rows then dividing by 2 to get the half way point. Next I use a while loop to loop through the mysql_fetch_array until the mid point is reached. I reset my counter variable to the midpoint then use a do....while loop to reach the other half of the rows. Now that it is done I feel that it was too simple of a task to have asked for help. Makes me feel dumb......lol
Pay no attention the echo's as that is where I will place the HTML code that I need looped. Current values are just for testing. Thank you again and here is the code:
Joshua
|

12-29-09, 07:02 PM
|
 |
Community Liaison
|
|
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
|
|
What you are trying to do is relatively simple.
I am using mysql_num_rows().
Something like this:
__________________
Jerry Broughton
Last edited by job0107; 12-29-09 at 07:04 PM.
|
|
The Following User Says Thank You to job0107 For This Useful Post:
|
|

12-30-09, 12:30 PM
|
|
Newbie Coder
|
|
Join Date: Dec 2009
Posts: 5
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by job0107
What you are trying to do is relatively simple.
I am using mysql_num_rows().
Something like this:
|
Jerry,
Your code is definately simpler than what I had come up with, but I am confused as to how some of your code is working. I know it works because I have tested it. I understand how your query works, I understand mysql_num_rows (which is a really cool function, BTW), and I understand your loop. Can you give me a little more explaination of the if statement. Correct me if I am wrong but the for loop starts at zero and loops to the max record. But how does the if statement know how to restart. I think that is where I am getting lost or at least that general area.
Again, your code is much more eloquent than what I came up with. That's why I must learn how it works. The only thing I would have done differently is echo out each line of HTML but that is just a personal visual preference. Thank you for a great reply.
Joshua
|

12-30-09, 02:43 PM
|
|
Aspiring Coder
|
|
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
|
|
I hope that wasn't to complicated of an explanation.
|
|
The Following User Says Thank You to Jcbones For This Useful Post:
|
|

12-30-09, 06:20 PM
|
|
Newbie Coder
|
|
Join Date: Dec 2009
Posts: 5
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by Jcbones
I hope that wasn't to complicated of an explanation.
|
Not complicated at all. Very informative actually. When I saw $sw was being reset to 1, that was blowing my mind because I thought the if statement was executing its code every loop. I also though that the if statement housed both of the echos: the one inline and the one below. That is why the $sw == 0 confused me. After your explaination I looked deeper to notice the "}" at the end of the inline echo. That is when it hit me that the inline echo only ran once at the mid point of the loop and started the second div/table. Once I realized that it was like reading the matrix...I didn't see code any more. I just saw blonde, brunett, red head......lol. Thanks a million for all of you guys chipping in and helping me with this issue.
Joshua
PS - Is there anyway to mark this thread as solved, complete, or should I worry about it?
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|