|
Joined: Apr 2001
Posts: 3,266
Member
|
Member
Joined: Apr 2001
Posts: 3,266 |
Okay I am pretty new to arrays and this is probally simple I have a query say and it pulls all information from a database. It creates a new tr row with the information and process's the next one. So when done there is a whole list of say 100 entried lets say. Now what If I wanted to break it up into two rows. Say I wanted to break up all results into two rows. Here is a code sample [code] echo <<<EXPRINT <table width="100%" cellpadding="0" cellspacing="0" border="0" align="center"> <tr> <td align="center" valign="top" width="34%"> <table> <tr> <td align="center" class="menubar"> Individual Members </td> </tr> EXPRINT; (query stuff goes here) while ( list($mem_firstname,$mem_lastname,$mem_affiliation,$mem_email) = (query handler)) ) { if ( $mem_email != "" ) { echo <<< EXPRINT <tr> <td align="center" class="$strColor"> <font class="medium"> <a href="mailto:$mem_email"> $mem_firstname $mem_lastname </a> </font> </td> </tr> EXPRINT; } else { echo <<< EXPRINT <tr> <td align="center" class="$strColor"> <font class="medium"> $mem_firstname $mem_lastname </font> </td> </tr> EXPRINT; } $strColor = switch_colors($strColor); } // Release the database handle (free database query); echo <<< EXPRINT </table> <br /><br /> EXPRINT;
|
|
|
|
Joined: Dec 2000
Posts: 1,471
Addict
|
Addict
Joined: Dec 2000
Posts: 1,471 |
And what is your question?  You should be fine as long as you echo out all information within the while loop.
|
|
|
|
Joined: Apr 2001
Posts: 3,266
Member
|
Member
Joined: Apr 2001
Posts: 3,266 |
LOL No the question was above Let me see if I can clarify. The code above will print out all results in one long list which is fine. However What I would like to do is actually split the results up into say two rows instead of one Example it fines 100 results. It will bring a table with 100 rows. Now what I want to do is actually take those 100 results and instead of one row have two rows of 50
|
|
|
|
Joined: Dec 2000
Posts: 1,471
Addict
|
Addict
Joined: Dec 2000
Posts: 1,471 |
Ok, I hope I understood it now. You'll have to fetch all mysql results into an array and use a loop to iterate over them. Example, assuming you're using the ubb.threads functions: // do the query $sth = $dbh -> do_query($query); // get the number of returned to know when you've to stop the loop $rows = $dbh -> total_rows($sth); // start the loop, do it until max rows is reached for ($i=0;$i < $rows ;$i++){ list ($yourvalue,$secondvalue) = $dbh -> fetch_array($sth); if ($i < ($rows / 2)) { // do something with the values } else { // do something else } } You can change the if condition to anything else. Note that are many ways to do it. Look at the user comments at http://de3.php.net/manual/de/function.mysql-fetch-array.php for different variants.
|
|
|
|
Joined: Apr 2001
Posts: 3,266
Member
|
Member
Joined: Apr 2001
Posts: 3,266 |
Okay Lets see if I understand this. I set where you have $i=0 to something lets say I know I have over 50 rows. So I set $i=25 So basically $i will be less than the total so I can tell it to do my normal way. However since this is a loop I a having a hard time figuring how to do the HTML for that. If you understand what I mean. If you look above on my normal while statement it just prints out table rows and then on the end I stick the closing </table>  Simple What I am having a problem understanding is since while your in the loop statement how would I be able to stop to be able after print those results to be able to specify another TD so I would be able to build that second row. Now ya understand what I am saying. I understand my normal code would go in the do something else after the else statement. If you go to http://www.masna.org/members.php you will see a test page I made up of our members. If you go to the bottom of the page you will see the individual members down the bottom and how they are real long. I want to make them two rows so the page is shorter.
|
|
|
|
Joined: Nov 2001
Posts: 10,369
I type Like navaho
|
I type Like navaho
Joined: Nov 2001
Posts: 10,369 |
No basically $rows is going to know that there are 50 things to output. $i is going to count them. Then basically inside the loop which is outputting results: Put this: if ($i < ($rows / 2)) { // do something with the values } Inside that "if" statement which only gets executed on #25 (the halfway point if you have 50) echo out a "</td><td>" which will start the new row.  Hope that helps. 
|
|
|
|
Joined: Apr 2001
Posts: 3,266
Member
|
Member
Joined: Apr 2001
Posts: 3,266 |
Ah so the
if ($i < ($rows / 2)) { // do something with the values
I put the html to split the td here }
Cool I think I understand now
Boy I am dumb sometimes
|
|
|
|
Joined: Apr 2001
Posts: 3,266
Member
|
Member
Joined: Apr 2001
Posts: 3,266 |
Okay I did your thing and it only printed out 22 entries and made them all a td which means they printed sideways. What I have is like 50 entries and want two rows of 25 Do I set $i to anything? Basically the html looks like this <table> <tr> <td> all the query coding etc goes here with the for statement blaH BLAH and the if statement and list () etc etc etc which prints out <table> <tr> <td> $results </td> </tr> </table then all loop closes etc } Further down then the table is closed out </td> </tr> </table> I used something like this with the code Astaran posted above so basically if what you are saying is correct that code I posted in that if statement would give me 2 rows right? if ($i < ($rows / 2)) { echo <<< UNSPRINT </td> <td align="center" class="lighttable"> UNSPRINT; } else { echo <<< UNSPRINT </td> <td align="center" class="lighttable"> UNSPRINT; } The page is 100% XHTML valid so its not bad html
|
|
|
|
Joined: Dec 2000
Posts: 1,471
Addict
|
Addict
Joined: Dec 2000
Posts: 1,471 |
[]What I have is like 50 entries and want two rows of 25 [/] So you want to have two columns in each html table?
<table> <tr> <td> values of the first mysql result row </td> <td> values of the second mysql result row </td> </tr> <tr> <td>values of the third mysql result row </td> <td>values of the fourth mysql result row</td> </tr> </table>
Or do you want to have two html tables with 25 rows in each? Or do you want to have one html table where the 25 rows have a blue background and the last 25 a green one?
Maybe you can give an example how the output should look like?
|
|
|
|
Joined: Apr 2001
Posts: 3,266
Member
|
Member
Joined: Apr 2001
Posts: 3,266 |
[] Omegatron said: If you go to http://www.masna.org/members.php you will see a test page I made up of our members. If you go to the bottom of the page you will see the individual members down the bottom and how they are real long. I want to make them two rows so the page is shorter. [/] I posted the code above in the first post. I gave the page to look at for suggestions. I want those real long list of individual members names to be divided in half. What is happening is instead of closing that first td and opening a new one after the half way entry it is printing a closing td and open td for every entry after the point. Maybe that explains it better. At any rate I want the resulting names split up into two lists if ($i < ($rows / 2)) { echo <<< UNSPRINT </td> <td valign="top" width="140" align="center" class="lighttable"> UNSPRINT; } else { echo <<< UNSPRINT </td> <td valign="top" width="140" align="center" class="lighttable"> UNSPRINT; }
|
|
|
|
Joined: Dec 2000
Posts: 1,471
Addict
|
Addict
Joined: Dec 2000
Posts: 1,471 |
[]I posted the code above in the first post. I gave the page to look at for suggestions. I want those real long list of individual members names to be divided in half. [/] Unfortunately, I can't access the page, so I'll try another guess:  Do you want something like this: <table> <tr> <td>Member1 <br>Member 2 <br> Member3 <br> Member 4 <br> Member 5</td> <td>Member6 <br>Member 7 <br> Member8 <br> Member 9 <br> Member 10</td> </tr> </table> Basically a table with two columns, were the first half is on the left side and the second half on right side? Or this: <table> <tr> <td>Member1</td><td>Member 2</td> </tr> <tr> <td>Member3</td><td>Member 4</td> </tr> ... // more rows <tr> <td>Member 9</td><td>Member 10</td> </tr> </table> A table with two columns and each entry in a own cell?
|
|
|
|
Joined: Apr 2001
Posts: 3,266
Member
|
Member
Joined: Apr 2001
Posts: 3,266 |
I want the second. <table> <tr> <td>Member1</td><td>Member 10</td> </tr> <tr> <td>Member2</td><td>Member 11</td> </tr> ... // more rows <tr> <td>Member 9</td><td>Member 20</td> </tr> </table> I have even taken the if statement and said if ( $i = ($rows / 2 ) ) { </td> <td> } So in theory I was thinking it would only print that after the half way part but it still prints every result in its own td
|
|
|
|
Joined: Dec 2000
Posts: 1,471
Addict
|
Addict
Joined: Dec 2000
Posts: 1,471 |
Ok, if you want to have have members 1-10 in one column, you can't output the html with a simple loop, because it always works from left to right. So you just put all results into an array. Your array looks like this: $array['0'] = "member1"; $array['1'] = "member2"; $array['2'] = "member3"; ... $array['19'] = "member20";
Next thing is to "reorganzie" the array so that every value is in the position you need it for the output. You could to that by splitting up the array into two different ones. One for the left column and one for the right column.
//You know the total number of results, so each array needs to store half of it $arraysize = $totalrows / 2 ; // first half results in the left array $leftarray = = array_slice ($array, 0,$arraysize); // second in the right array $leftarray = = array_slice ($array, $arraysize); for ($i=0;$i < $totalrows /2 ;$i++){ echo "<tr><td>$leftarray[$i]</td><td>$leftarray[$i]</td></tr> }
A different solution would be to use the result array and do some calculations which array element needs to be printed. Basically, you'd do: $rowstoprint = $totalrows / 2 ; for ($i=0;$i < $totalrows /2 ;$i++){ echo "<tr><td>$result[$i]</td><td>$result[$i + $rowstoprint]</td></tr> }
|
|
|
|
Joined: Apr 2001
Posts: 3,266
Member
|
Member
Joined: Apr 2001
Posts: 3,266 |
OKay well I got a little closer. Now I don't know exact row so your second suggestion sounds best
Here is the code that prints all of them in one row so maybe you can suggest how to split them into two as I tried your code above and it did not work or I muffed it up
$rows = $oSQL->DBRowCount($DBResource);
for ($i=0;$i < $rows ;$i++){
list($mem_firstname,$mem_lastname,$mem_email) = $oSQL->DBFetchArray($DBResource);
if ( $mem_email != "" ) { echo <<< UNSPRINT <tr> <td align="center" class="$strColor"> <font class="medium"> <a href="mailto:$mem_email"> $mem_firstname $mem_lastname </a> </font> </td> </tr> UNSPRINT; } else { echo <<< UNSPRINT <tr> <td align="center" class="$strColor"> <font class="medium"> $mem_firstname $mem_lastname </font> </td> </tr> UNSPRINT; }
}
|
|
|
|
Joined: Dec 2000
Posts: 1,471
Addict
|
Addict
Joined: Dec 2000
Posts: 1,471 |
Can you perform the followin command and limit the query so that only 3 results are returned? I need to know how the DBFetchArray functions returns the values.
print_r($oSQL->DBFetchArray($DBResource));
Beside that, you know the number the returned rows: $rows = $oSQL->DBRowCount($DBResource);
|
|
|
|
Joined: Apr 2001
Posts: 3,266
Member
|
Member
Joined: Apr 2001
Posts: 3,266 |
Array ( [0] => Robert [FirstName] => Robert [1] => Baker [LastName] => Baker [2] => [] [email protected][/] [Email] => [] [email protected][/] ) That is what your looking for if ( $mem_email != "" ) { echo <<< UNSPRINT <tr> <td align="center" class="$strColor"> <font class="medium"> <a href="mailto:$mem_email"> $mem_firstname $mem_lastname </a> </font> </td> </tr> UNSPRINT; } else { echo <<< UNSPRINT <tr> <td align="center" class="$strColor"> <font class="medium"> $mem_firstname $mem_lastname </font> </td> </tr> UNSPRINT; } I use that above to make it look like this Robert Baker with his email address as an <a href tag So as I am asking there are 3 arrays in every row but I already define how they are displayed. I need to take the total rows and divide them into two columns. I hope that is clear enough.
|
|
|
|
Joined: Dec 2000
Posts: 1,471
Addict
|
Addict
Joined: Dec 2000
Posts: 1,471 |
// get the number of results that are returned by the query $totalrows = $oSQL->DBRowCount($DBResource); // load the result into an array while ($row = $oSQL->DBFetchArray($DBResource);) { $result[] = $row; } // calculate how many table rows we need to display all results $rowstoprint = $totalrows / 2 ; // make a loop to output every html row of the table for ($i=0;$i < $totalrows /2 ;$i++){ // second counter - calculate the right index for the right column $j = $i + $rowstoprint; echo "<tr><td>"; if ($result[$i]['Email'] != "" ) { echo "<a href="mailto:$result[$i]['Email']">"; } echo "$result[$i]['FirstName'] $result[$i]['LastName']</td><td>" if ($result[$j]['Email'] != "" ) { echo "<a href="mailto:$result[$j]['Email']">"; } echo "$result[$j]['FirstName'] $result[$j]['LastName']</td></tr>"; }
Last edited by Astaran; 01/20/2004 2:42 PM.
|
|
|
|
Joined: Apr 2001
Posts: 3,266
Member
|
Member
Joined: Apr 2001
Posts: 3,266 |
Okay that doesnt print anthing but ['FirstName'] and ['LastName'] in
two columns but no actual information.
['FirstName']
|
|
|
|
Joined: Dec 2000
Posts: 1,471
Addict
|
Addict
Joined: Dec 2000
Posts: 1,471 |
 Forgot to copy the while loop to load everything into the array. Added it in the above post.
|
|
|
|
Joined: Apr 2001
Posts: 3,266
Member
|
Member
Joined: Apr 2001
Posts: 3,266 |
Cool I was able to figure it out the problems. You had a couple ; left off and the variables needed to be surrounded by { } but I got it to work like this $totalrows = $oSQL->DBRowCount($DBResource); // load the result into an array while ($row = $oSQL->DBFetchArray($DBResource)) { $result[] = $row; } // calculate how many table rows we need to display all results $rowstoprint = $totalrows / 2 ; // make a loop to output every html row of the table for ($i=0;$i < $totalrows /2 ;$i++){ // second counter - calculate the right index for the right column $j = $i + $rowstoprint; echo <<< UNSPRINT <tr> <td align="center" class="$strColor"> UNSPRINT; if ( $result[$i]['Email'] != "" ) { echo <<< UNSPRINT <font class="medium"> <a href="mailto:{$result[$i]['Email']}"> {$result[$i]['FirstName']} {$result[$i]['LastName']} </a> </font> UNSPRINT; } else { echo <<< UNSPRINT <font class="medium"> {$result[$i]['FirstName']} {$result[$i]['LastName']} </font> UNSPRINT; } echo <<< UNSPRINT </td> <td align="center" class="$strColor"> UNSPRINT; if ( $result[$j]['Email'] != "" ) { echo <<< UNSPRINT <font class="medium"> <a href="mailto:{$result[$j]['Email']}"> {$result[$j]['FirstName']} {$result[$j]['LastName']} </a> </font> UNSPRINT; } else { echo <<< UNSPRINT <font class="medium"> {$result[$j]['FirstName']} {$result[$j]['LastName']} </font> UNSPRINT; } echo <<< UNSPRINT </td> </tr> UNSPRINT; } Only thing I see though is that my last result on row 1 match's the first result on row 2 So not perfect but close http://www.masna.org/members.php down the bottom in individual members.
|
|
|
|
Joined: Dec 2000
Posts: 1,471
Addict
|
Addict
Joined: Dec 2000
Posts: 1,471 |
Ok, last post for now.  change $j = $i + $rowstoprint; to $j = $i + ceil($rowstoprint);
|
|
|
Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.
Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
|
|
Posts: 417
Joined: November 2001
|
|
Forums63
Topics37,575
Posts293,930
Members13,823
|
Most Online6,139 Sep 21st, 2024
|
|
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
|
|
|
|