Take 10 array elements at a time in php? - php

echo "<table title='mxit:table:full' style='width: 100%' width='100%'><colgroup span='2' width='50%'></colgroup>";
foreach($arr['chart_data'] as $key => $element){
echo "<tr>";
foreach($element as $subkey => $subelement){
// $subelement =chop($subelement,'DIRECTSegment');
if($subkey++ < 2) {
if($key == 0)
{
echo "<td align='center;' style='color:white;'>$subelement</td>";
}
else if($subkey == 1)
{
echo "<td align='center;' style='color:white;'>$subelement</td>";
}
else
{
echo "<td align='center;' style='color:white;'><a href='getdata.php?key=$key'>".$subelement."</a></td>";
}
}
}
}
echo "</tr>";
echo "</table>";
How do i take only 10 elements of my array $arr['chart_data'] at a time ?

Set a counter, and then break the loop when $count hits 10.
$count = 0;
/* loop here */
if ($count == 10) break;

Anyway for all those who gave me negative votes and for my own betterment i solved my issue:
Here is what i did:
$totalfiles = count($arr['chart_data']);
$limit = 10;
$pages = ceil($totalfiles / $limit);
if (!isset($_GET['startrow']) or!is_numeric($_GET['startrow'])) {
//we give the value of the starting row to 0 because nothing was found in URL
$startrow = 0;
//otherwise we take the value from the URL
} else {
$startrow = (int) $_GET['startrow'];
}
echo "<table title='mxit:table:full' style='width: 100%' width='100%'><colgroup span='2' width='50%'></colgroup>";
$data = array_slice($arr['chart_data'], $startrow, 10); // same as offset 0 limit 50 in sql
foreach($data as $key => $element) {
echo "<tr>";
foreach($element as $subkey => $subelement) {
$subelement = chop($subelement, 'DIRECTSegment');
if ($subkey++ < 2) {
if ($key == 0 && $startrow == 0) {
echo "<td align='center;' style='color:white;'>$subelement</td>";
} else if ($subkey == 1) {
echo "<td align='center;' style='color:white;'>$subelement</td>";
} else {
echo "<td align='center;' style='color:white;'><a href='getdata.php?key=$key'>".$subelement.
"</a></td>";
}
}
}
echo "</tr>";
}
echo "</table>";
/* free result set */
$result - > close();
}
/* close connection */
$mysqli - > close();
//now this is the link..
echo '<a href="'.$_SERVER['PHP_SELF'].
'?startrow='.($startrow + 10).
'">Next</a>';
$prev = $startrow - 10;
//only print a "Previous" link if a "Next" was clicked
if ($prev >= 0)
echo ' <a href="'.$_SERVER['PHP_SELF'].
'?startrow='.$prev.
'">Previous</a>';
Implemented paging and using array slice to display 10 elements at a time.
Thanks for all the help

Related

Make new table after reaching a certain row count

I created a code that converts characters to binary and make table cells black/white corresponding to the ones and zeros. This is my code:
$str_splt = str_split($text);
echo "<table>";
for ($a=0;$a < count($str_splt);$a++) {
$bits = array(128,64,32,16,8,4,2,1);
$store = array(0,0,0,0,0,0,0,0);
$inp = ord($str_splt[$a]);
for ($x=0;$x < count($bits);$x++) {
if ($bits[$x] <= $inp) {
$inp = $inp - $bits[$x];
$store[$x] = 1;
} else {
$store[$x] = 0;
}
};
$store_rvs = array_reverse($store);
echo "<tr>";
for ($b=0;$b < count($store_rvs);$b++) {
if ($store_rvs[$b] == '1') {
echo "<td id=\"blk\"></td>";
}
else {
echo "<td></td>";
}
}
echo "</tr>";
}
echo "</table>";
Its output looks like this ($text = "ABCDEFGH"):
As you can see it's 8x8 table. I want to add the next set of bytes to the side of that table like this:
Each 8x8 table is a group. The two images above is group 1 and group 2:
I want to display the tables like this but I can't find the solution.
I did it in this way. Ignore my css if you are fine with yours. I replaced the id tag with class because each id should be defined once only.
echo "<html><head>";
echo "<style type='text/css'>";
echo " table, td { padding:0px; margin:0px; }";
echo " td.cell { width:15px; height:15px; }";
echo " td.blk { background-color:black; }";
echo " td.wht { background-color:yellow; }";
echo "</style>";
echo "</head><body>";
$text = "ABCDEFGH";
$text.= "ABCDEFGH";
echo "<table><tr><td><table>";
for($a=0; $a<strlen($text); $a++) {
$chr = substr($text,$a,1);
$bits = array(128,64,32,16,8,4,2,1);
$store = array(0,0,0,0,0,0,0,0);
$inp = ord($chr);
for($x=0; $x<count($bits); $x++) {
if($bits[$x] <= $inp) {
$inp = $inp - $bits[$x];
$store[$x] = 1;
} else {
$store[$x] = 0;
}
}
$store_rvs = array_reverse($store);
if($a % 8 === 0) {
echo "</table></td><td><table>";
}
echo "<tr>";
for($b=0; $b<count($store_rvs); $b++) {
if($store_rvs[$b] == '1') {
echo "<td class='cell blk'></td>";
} else {
echo "<td class='cell wht'></td>";
}
}
echo "</tr>";
}
echo "</table></td></tr></table>";

mysqli/PHP - first two rows never show up(table with row data going across columns then moving down)

My website currently ignores the first two images you place into the database and then proceeds to add images going across 5 columns and then moving down to the next row.
Update: Now it shows 3 of the 4 images in the database. Skips one image.
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
This is what my database looks like
http://i.stack.imgur.com/IFba8.jpg
This is what my website shows
http://i.stack.imgur.com/Wf7E1.jpg
Try this:
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
Please try this.
<?php
$i = 1;
echo "<table>";
while ( $row = $Recordset2->fetch_object() ) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>

How to show the value of two related arrays in php?

I have two arrays myarray1 has name of images and myarray2 has the address of images,
I am going to show image names and their addresses in pagination but do not know how to complete the code.
$pages = array_chunk($myarray1,10);
$addrs = array_chunk($myarray2,10);
$page_number = 1
$count = 0;
echo'<table>';
echo'<tr>';
foreach ($pages[$page_number] as $i) {
$counter++;
if ($count == 5) {
echo '</tr><tr>';
$counter = 0;
}
echo'<td>'.$i. " AND " . <<Value of addrs array goes here
echo'</td>';
}
.......
I'll do it on this way
$count = 1;
foreach ($pages[$page_number] as $key => $i) {
if ($count % 5 == 0) {
echo '</tr><tr>';
echo '</div>';
echo '</div>';
}
echo'<td>'.$i. " AND " . $addrs[$page_number][$key]
echo'</td>';
$count++; // increase at the end.
}
if you can try to var_dump those arrays before foreach just to be clear how structure looks like.
Try something like this: (note also the counter++ is changed in $count++)
foreach ($pages[$page_number] as $key => $i) {
$count++;
if ($count == 5) {
echo '</tr><tr>';
echo '</div>';
echo '</div>';
$count = 0;
}
echo'<td>'.$i. " AND " . $addrs[$page_number][$key]
echo'</td>';
}

php mysql_fetch_array every 25 items wrap a div

I want to mysql_fetch_array 100 items from mysql database. then every 25 items wrap a div.
also, I make a total items result check.
My code as below, but it will add <div> to every item, no as my require. So how to make it easier? Thanks.
if (mysql_num_rows($result) != 0) {
$total = mysql_num_rows($result);
$num = 1;
while ($row = mysql_fetch_array($result)) {
if ($num === 1) {
echo '<div>';
}
echo $row['title'] . '<br />';
if ($total < 26) {
echo '</div>';
}
else {
if ($num === 26) {
echo '<div>';
}
if ($total < 51) {
echo '</div>';
}
else {
if ($num === 51) {
echo '<div>';
}
if ($total < 76) {
echo '</div>';
}
else {
if ($num === 75) {
echo '<div>';
}
if ($total < 101) {
echo '</div>';
}
}
}
}
$num++;
}
}
You can use the mod operator.
See: http://php.net/manual/en/internals2.opcodes.mod.php
echo '<div>';
while($row = mysql_fetch_array($result)){
....
if (($num % 25) === 1) { echo '</div><div>' }
$num++;
}
echo '</div>';
Try This...
if(mysql_num_rows($result)!=0){
$total = mysql_num_rows($result);
$num=1;
while($row = mysql_fetch_array($result)){
if($num===1)
echo '<div>';
echo $row['title'].'<br />';
if($num==25){
echo '</div>';
$num=1;
}
$num++;
}
}
use the mod operator.
$num =1;
while($row = mysql_fetch_array($result)){
if (($num % 25) === 1) { echo '<div>' }
-------here data ----
if (($num % 25) === 0) { echo '</div>' }
$num++;
}
My advice is to separate your concerns... Give it a shot more like this:
// first get all your data.
$results = array();
while($row = mysql_fetch_array($result)){
$results[] = $row;
}
// now you have an array of all of your records.
if (!empty($results)) {
// total count of the array up front.
$total = count($results);
$interval = 0;
// save the data in a buffer for echoing later.
$buffer = array('<div>');
foreach($results as $row) {
$buffer[] = $row['title'] . '<br />';
if (++$interval === 24) { // notice the prefix ++
$interval = 0;
// close and re-open divs
$buffer[] = '</div><div>';
}
}
// one final close tag
$buffer[] = '</div>';
// now we zip it up and echo the content.
echo implode('', $buffer);
}
Here is how I do it...
$count = 0;
while($row = mysql_fetch_array($result)){
if ($count%$25 == 0)
{
echo "<div>";
}
//whatever your data goes here
count++
if ($count%$25 == 0)
{
echo "</div>";
}
}

PHP looping problem?

I have this script that displays a max of 5 images for each row, but for some reason my <ul> tag won't close correctly if the number of items isn't an exact multiple of 5. How can I correct this problem so the <ul> tag will close even if the number of listed images is less then 5?
Here is my PHP code.
if (!$dbc) {
print mysqli_error($mysqli);
} else {
$row_count = 0;
while($row = mysqli_fetch_array($dbc)){
if($row_count % 5 == 0){
echo "<ul>";
}
echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
echo "<img src='".$row['src']."'></a></li>";
if($row_count % 5 == 4) {
echo "</ul>";
}
$row_count++;
}
}
below the loop, check if
if (!$dbc) {
print mysqli_error($mysqli);
} else {
$row_count = 0;
while($row = mysqli_fetch_array($dbc)){
if($row_count % 5 == 0){
echo "<ul>";
}
echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
echo "<img src='".$row['src']."'></a></li>";
if($row_count % 5 == 4) {
echo "</ul>";
}
$row_count++;
}
if ( (($row_count % 5) > 0) && (($row_count % 5) < 4))
echo "</ul>";
}
$multiple = false;
if (!$dbc) {
print mysqli_error($mysqli);
} else {
$row_count = 0;
while($row = mysqli_fetch_array($dbc)){
if($row_count % 5 == 0){
echo "<ul>";
}
echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
echo "<img src='".$row['src']."'></a></li>";
if($row_count % 5 == 4) {
$multiple = true;
echo "</ul>";
} else {
$multiple = false;
}
$row_count++;
}
if($multiple == false) {
echo "</ul>";
}
}
if (!$dbc) {
print mysqli_error($mysqli);} else {
$row_count = 0;
//tank start
$total_rows = mysqli_num_rows($dbc);
//tank end
while($row = mysqli_fetch_array($dbc)){
if($row_count % 5 == 0){
echo "<ul>";
}
echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
echo "<img src='".$row['src']."'></a></li>";
//tank start
if($row_count % 5 == 4 || $row_count==$total_rows) {
//tank end
echo "</ul>";
}
$row_count++;
}
Here's my updated solution. I think it looks a little clearer. I do setup a few variables to get rid of some IF statements, and replace them with FOR loops. Mainly for readability and it's the way i thought of doing it.
$itemsperrow = 5;
$items = mysqli_num_rows($dbc);
$rows = $items / $itemsperrow;
$itemcount = 0;
if (!$dbc)
{
echo(mysqli_error($mysqli));
}
else
{
for ($int = 0; $int < $rows; $int++)
{
echo "<ul>";
for ($item = 0; $item < $itemsperrow; $item++)
{
if ($itemcount >= $items)
{
echo "</ul>";
exit;
}
else
{
$row = mysqli_fetch_array($dbc);
echo "<li><a href='". $row["url"] . "'>";
echo "<img src='" . $row["src"] . "' title='" . $row["title"] . "'/></a></li>";
$itemcount++;
}
}
echo "</ul>";
}
}

Categories