• Register
Welcome to phpanswers , where you can ask questions and receive answers from other members of the community.

How do i display data from a MYSQL Database in a table in the four rows, row has first record, row 2 has next 3 records, row 3 has the next 4 records and row 4 has the last 3 records

0 votes
How do i display data from a MYSQL Database in a table with four rows, First row has the first record, row 2 has next 3 records, row 3 has the next 4 records and row 4 has the last 3 record. Total 11 records.
asked 1 year ago by anonymous

1 Answer

0 votes

Select the records from the table using the query and put it in an indexed array.

 

After this use the PHP array function array_slice

 

The array_slice() function returns selected parts of an array.

 

example:

<?php
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,1,2));
?>

The output of the code above will be:

Array ( [0] => Cat [1] => Horse )
 

With string keys:

<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse","d"=>"Bird");
print_r(array_slice($a,1,2));
?>

The output of the code above will be:

Array ( [b] => Cat [c] => Horse )
 
By this way we can split the needed results array  like this
 
Let $a as our results array
 
$firstrow= array_slice($a,0,1);
$secondrow= array_slice($a,1,3);
$thirdrow= array_slice($a,4,4);
$fourthrow= array_slice($a,-1,3);
 
 
 
answered 1 year ago by anonymous
edited 1 year ago by anonymous

Related questions