Question:
PHP and MySQL programming help?
Vertigo
2007-07-31 08:21:59 UTC
I am trying to align some variables from a MySQL query like this:

1 5
2 6
3 7
4 8

but I can only do this:

1 2
3 4
5 6
7 8

Does anyone know how I can do the first example. Here is my code so far for the second example:

while( $row=mysql_fetch_array($sql)) {

extract($row);
echo "

$id
$user ";

$row=mysql_fetch_array($sql);
extract($row);
echo "

$id
$user
";
}
Three answers:
Big D
2007-07-31 09:04:47 UTC
if you want to have the columns split in an even order you will need to count the returned results first:



$countRecords = "SELECT COUNT(id) as totalRecords FROM Table WHERE blahblah=$blahblah";



user the same query you run to print out the info except with count(id) as totalRecords instead - if you are joining the tables make sure you group by the table with the main records



$result = mysql_query($countRecords);

$resultsPerColumn = mysql_result($result, 0);



you could also use, instead of COUNT(id) just use id and use the mysql_num_rows function instead.



Ok once you get the result of the count divide

$resultsPerColumn = CEIL($totalResults/2);



this will give you the amounts on each side.

now initialize a variable at 0 & 2 other variables like

$countRecords = 0;

$firstColumn = ''

$secondColumn = '
'



From here run your query like you did

while( $row=mysql_fetch_array($sql)) {



if($countRecords < $resultsPerColumn ){

$firstColumn .= '';

$countRecords++;

}

else{

$secondColumn .= '';

}



}



$firstColumn .= "
' . $row['id'] . '' . $row['user'] . '
' . $row['id'] . '' . $row['user'] . '
";

$secondColun .= "";

?>



























Now is it worth it to do this if you can avoid it? Probably not, but if you need to you can do it this way.



This is just one way I'm sure, and im sure its not the most efficient but its an answer for you.
Sam
2007-07-31 09:00:53 UTC
Assuming you will not always have 8 records in your database, create a SQL query that gets a COUNT(*). Then divide that count by 2 (or the number of columns you wish to have).



Then, create a counter variable and increment it by one inside your while loop. When the counter is greater than (the total record count / 2), reset your counter, close the previous structure and create a new one.



If your
has an align="left" property then your tables will sit side-by-side... example output:













1one
2two
3three
4four














5five
6six
7seven
8eight






not sure about the code itself, but it would be something like this:

------------------------------------------------------------



$counter=0;

while( $row=mysql_fetch_array($sql)) {

$counter++;



extract($row);

echo "



$id

$user

";



if ($counter > ($total_recs / 2)) {

$counter = 0;

echo "";

}



}
happ
2016-11-11 00:39:24 UTC
use the password for the person 'peter@localhost'. supply the password in $con=mysql_connect("localhost","puppy... this ought to resolve your problem. on the different hand you may edit the mysql database using kit PHPmyadmin and alter as Password='No' for the person, 'peter@localhost'.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...