Morning,
I have some code that takes a string of shoe sizes, explodes it and then puts it in a table. The code works perfectly and the info is displayed correctly on the page but after the table there is always a few 1's after the table. I'm really confused about this.
So the table looks like this (in columns of three on the webpage)
Available in the following sizes:
3 4 5
6 9 10
111
Here's the Codes, for the purpose of the example $x = 3|4|6|10|9
function get_sizes_pipe($x) {
$counter = 0;
$splits = explode("|",$x);
asort($splits);
$x = print "<table class='greentable'>";
$x .= print "<thead><tr><th scope='col' colspan='3' abbr='Starter'>Available in the following sizes:</th></tr></thead><tbody><tr>";
foreach ($splits as $split) {
$entry = print "<td>".$split."</td>";
if($counter == 2){
$entry .= print "</tr><tr>";
$counter =0;
} else {
$counter++;
}
}
if($counter == 1){
$entry .= print "<td></td><td></td>";
}elseif ($counter == 2) {
$entry .= print "<td></td>";
}
$x.= $entry;
$x.= print "</tr></tbody></table>";
return $x;
}
I'd really appreciate any help.
Thanks
Chris
I don't know why do you concatenate print with some variable, but you should know that function print returns 1 (always) as a result of execution, so doing
$x .= print 'some value';
is the same as
print 'some value';
$x .= 1;
try this:do not use print you are already appending it to $x just echo $x;
<?php
function get_sizes_pipe($x) {
$counter = 0;
$splits = explode("|",$x);
asort($splits);
$x = "<table class='greentable'>";
$x .= "<thead><tr><th scope='col' colspan='3' abbr='Starter'>Available in the following sizes:</th></tr></thead><tbody><tr>";
foreach ($splits as $split) {
$entry .= "<td>".$split."</td>";//use . to concate here
if($counter == 2){
$entry .= "</tr><tr>";
$counter =0;
} else {
$counter++;
}
}
if($counter == 1){
$entry .="<td></td><td></td>";
}elseif ($counter == 2) {
$entry .= "<td></td>";
}
$x.= $entry;
$x.="</tr></tbody></table>";
echo $x;
}
get_sizes_pipe("1|2|3|4");
Rather than using string concatenation ( which becomes inefficient with large strings ) add html strings to an array and return the flattened html from your function.
function get_sizes_pipe($x) {
$counter = 0;
$splits = explode("|",$x);
asort( $splits );
$html=array();
$html[]="
<table class='greentable'>
<thead>
<tr>
<th scope='col' colspan='3' abbr='Starter'>Available in the following sizes:</th>
</tr>
</thead>
<tbody>
<tr>";
foreach( $splits as $split ) {
$html[]="<td>".$split."</td>";
if( $counter == 2 ){
$html[]="</tr><tr>";
$counter=0;
} else {
$counter++;
}
}
$html[]=( $counter == 1 ) ? "<td></td><td></td>" : "<td></td>";
$html[]="
</tr>
</tbody>
</table>";
return implode( PHP_EOL, $html );
}
/* call your function with whatever value of `$x` you need */
echo get_sizes_pipe( $what_is_x );
Related
How can I print a chess table in PHP without using for loops, is it possible to do it with conditional and logical statements only?
<table width = "270px" cellspacing = "0px" cellpadding = "0px" border = "1px" bordercolor = "gray">
<?php
$value = 0;
for($col = 0; $col < 8; $col++) {
echo "<tr>";
$value = $col;
for($row = 0; $row < 8; $row++) {
if($value%2 == 0) {
echo
"<td height=40px width=20px bgcolor=black></td>";
$value++;
}
else {
echo
"<td height=40px width=20px bgcolor=white></td>";
$value++;
}
}
echo "</tr>";
}
?>
You could use str_repeat:
<table width="270px" cellspacing="0" cellpadding="0" border="1px" bordercolor="gray">
<?php
$td = '<td height="40px" width="20px" bgcolor=';
$black = "$td'black'></td>";
$white = "$td'white'></td>";
echo str_repeat("<tr>" . str_repeat($black . $white, 4) . "</tr><tr>"
. str_repeat($white . $black, 4) . "</tr>", 4);
?>
</table>
One solution is to use build-in loops derived from some functions, here array_walk(only to avoid explicit for).But it the end the algorithm is the same. It's only a trick !
<?php
$arr = array_fill(0,64,1);
array_walk($arr,"construct");
array_walk($arr,"display");
function construct(&$item, $key)
{
$color=$key%2;
$i=($key-$key%8)/8 + 1;
if($i%2==0)
$color = ($color+1)%2;
$color = $color%2==0 ? "white" : "black";
$j=$key%8+1;
$item = new Square($i,$j,$color);
}
function display($item, $key)
{
if($key%8==0) echo "<br>";
echo $item." ";
}
class Square
{
public $i;
public $j;
public $color;
public function __construct($i,$j,$color)
{
$this->i=$i;
$this->j=$j;
$this->color=$color;
}
public function __toString()
{
return "[".$this->i.",".$this->j."]=".($this->color);
}
}
?>
Output:
[1,1]=white [1,2]=black [1,3]=white [1,4]=black [1,5]=white [1,6]=black [1,7]=white [1,8]=black
[2,1]=black [2,2]=white [2,3]=black [2,4]=white [2,5]=black [2,6]=white [2,7]=black [2,8]=white
[3,1]=white [3,2]=black [3,3]=white [3,4]=black [3,5]=white [3,6]=black [3,7]=white [3,8]=black
[4,1]=black [4,2]=white [4,3]=black [4,4]=white [4,5]=black [4,6]=white [4,7]=black [4,8]=white
[5,1]=white [5,2]=black [5,3]=white [5,4]=black [5,5]=white [5,6]=black [5,7]=white [5,8]=black
[6,1]=black [6,2]=white [6,3]=black [6,4]=white [6,5]=black [6,6]=white [6,7]=black [6,8]=white
[7,1]=white [7,2]=black [7,3]=white [7,4]=black [7,5]=white [7,6]=black [7,7]=white [7,8]=black
[8,1]=black [8,2]=white [8,3]=black [8,4]=white [8,5]=black [8,6]=white [8,7]=black [8,8]=white
For html grid, just adapt display.
function displayHtml($item, $key)
{
if($item->i==1 && $item->j==1)
echo '<table width="270px" cellspacing="0" cellpadding="0" border="1px" bordercolor="gray">';
if($item->j%8==1)
echo "<tr>";
echo "<td height=40px width=20px style='background-color:".$item->color."'></td>";
if($item->j%8==0)
echo "</tr>";
if($item->i==8 && $item->j==8)
echo "</table>";
}
I'm trying out the modulus division with a foreach loop, and I'm having a bit trouble understanding it.
$counter = 0;
foreach($result as $row){
if(isset($row['username'])){
if (($counter) % 2 == 0){
echo "<tr class=\"r1\"><td class=\"center\">" . $row['username'] . "</td></tr>";
}
else{
echo "<tr class=\"r0\"><td class=\"center\">" . $row['username'] . "</td></tr>";
}
$counter++;
}
}
I want to output:
<tr class="r0">
<td>Bob</td>
<td>Daniel</td>
</tr>
<tr class="r1">
<td>Dylan</td>
<td>Bruce</td>
</tr>
But currently, with my loop, I'm outputting:
<tr class="r1">
<tdBob</td>
</tr>
<tr class="r0">
<td>Daniel</td>
</tr>
<tr class="r1">
<td>Dylan</td>
</tr>
<tr class="r0">
<td>Bruce</td>
</tr>
Can someone explain to me exactly how modulus division works? Thank you.
Here, you want to display two records in one row. Actually modulus division will return the remainder of the division. Try with:
$counter = 0;
$i=1;
foreach($result as $row){
if(isset($row['username'])){
if (($counter) % 2 == 0){ // that is the counter value/2 doesnot returns a remainder ie, divisible by 2, then create another row
if($i%2==0)
{
echo "<tr class=\"r1\">";
}
else
{
echo "<tr class=\"r0\">";
}
}
else{
echo "<td class=\"center\">" . $row['username'] . "</td>";
}
if (($counter) % 2 == 0){ // close the tr if the counter doesnot return remainder
echo "</tr>";
}
$counter++;
$i++;
}
}
I got this to work. My problem was that my array was multidimensional, so I converted it to a single array. Afterwards, I used array_chunks.
$chunks = array_chunk($l, 2);
$i=0;
foreach($chunks as $mychunk){
if($i%2== 0){
echo "<tr class=\"r0\">";
} else { echo "<tr class=\"r1\">"; }
$i++;
foreach($mychunk as $newchunk)
{
echo "<td class=\"center\">" . $newchunk . "</td>";
}
echo "</tr>";
}
For anyone looking to convert multidimensional arrays into single dimensional array:
function array_flatten($array) {
if (!is_array($array)) {
return FALSE;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
}
else {
$result[$key] = $value;
}
}
return $result;
}
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>';
}
I've got a table that is default sorted ascendingly by its 2nd column by numeric value.
I'm trying to grab the top 5 rows from this table, but my script doesn't seem to want to follow the tablesorter forced sort order I have, it's just taking the default non sorted data.
So how do I get my script to grab the sorted table data? I don't want to have to grab all the data and sort it again for this script.
here is my code
<?php
include("bestbrokers_array.php");
require('simple_html_dom.php');
$table = array();
$html = file_get_html('http://www.forexfbi.com/best-forex-brokers-comparison/');
$num=1;
foreach($html->find('tr') as $row) {
if($num <= 6)
{
$rating = $row->find('td',1)->innertext;
$name = $row->find('td',0)->plaintext;
$table[$name][$rating] = true;
$num++;
}
}
html_show_array($table);
?>
and..
<?php
function do_offset($level){
$offset = ""; // offset for subarry
for ($i=1; $i<$level;$i++){
$offset = $offset . "<td></td>";
}
return $offset;
}
function show_array($array, $level, $sub){
if (is_array($array) == 1){ // check if input is an array
foreach($array as $key_val => $value) {
$offset = "";
if (is_array($value) == 1){ // array is multidimensional
echo "<tr>";
$offset = do_offset($level);
echo $offset . "<td>" . $key_val . "</td>";
show_array($value, $level+1, 1);
}
else{ // (sub)array is not multidim
if ($sub != 1){ // first entry for subarray
echo "<tr nosub>";
$offset = do_offset($level);
}
$sub = 0;
echo $offset . "<td main ".$sub." width=\"120\">" . $key_val .
"</td>";
echo "</tr>\n";
}
} //foreach $array
}
else{ // argument $array is not an array
return;
}
}
function html_show_array($array){
echo "<table cellspacing=\"0\" border=\"2\">\n";
show_array($array, 1, 0);
echo "</table>\n";
}
?>
Lets say I have an array
$aSomeArray = array("1","2","3","4","5","6","7","8","9","10","11","12");
Now displaying one array value in one table row is easy like this
echo "<table>";
foreach ($aSomeArray as $iSomeArrayKey => $iSomeArrayValue)
{
echo "<tr>";
echo "<td>".$iSomeArrayValue."</td>";
echo "</tr>";
}
echo "</table>";
But I want to display the values in table format like this
1 2 3 4
5 6 7 8
9 10 11 12
How can I achieve this ?
Edited, this works:
$aSomeArray = array("1","2","3","4","5","6","7","8","9","10","11","12");
$i = 0;
echo "<table>\r\n";
foreach ($aSomeArray as $aSomeArrayKey => $aSomeArrayValue)
{
if (($i % 4) == 0) echo "\t<tr>\r\n";
echo "\t\t<td>" . $aSomeArrayValue . "</td>\r\n";
if (($i % 4) == 3) echo "\t</tr>\r\n";
$i++;
}
echo "</table>\r\n";
Just a thought which seems nicer IMHO.
<?php
$aSomeArray = array("1","2","3","4","5","6","7","8","9","10","11","12");
function createMatrix($width, $array)
{
$newArray = array();
$temp = array();
$count = 1;
foreach($array as $key => $value)
{
$temp[] = $value;
if(($count++ % $width) == 0)
{
$newArray[] = $temp;
$temp = array();
}
}
if( count($temp) > 0)
{
$newArray[] = $temp;
}
return $newArray;
}
will create a matrix array of variable $width
You can then use that data as a double for-each like this:
$matrix = createMatrix(2, $aSomeArray );
foreach($matrix as $row)
{
echo "<tr>\n";
foreach($row as $td)
{
echo "\t<td>{$td}</td>\n";
}
echo "</tr>\n";
}
Which produces:
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
</tr>
<tr>
<td>11</td>
<td>12</td>
</tr>
Just do a running count. So before the foreach set $i = 1 and every fourth result, reset the count to $i = 1 and then end the row and re-open a new row.
$aSomeArray = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13");
$columns = 4; // The number of columns to shown
echo "<table>";
$i = 0;
$trOpen = false; // just a flag if <tr> has been closed (paired with </tr>) or not.
foreach ($aSomeArray as $item) {
if ($i % $columns == 0) {
echo "<tr>";
$trOpen = true;
}
echo "<td>" . $item . "</td>";
if (($i + 1) % $columns == 0) {
echo "</tr>";
$trOpen = false;
}
$i++;
}
if ($trOpen) {
echo "</tr>"; // add '</tr>' if it is not yet added.
}
echo "</table>";
$i=1;
echo "<tr>";
foreach ($aSomeArray as $aSomeArrayKey => $aSomeArrayValue)
{
echo "<td>".$aSomeArrayValue."</td>";
$i++;
if($i%4==0){
echo "<tr></tr>";
}
}
echo "</tr>";
It will also work for the indivisible by 4 number of elements.
echo "<table>";
echo "<tr>";
foreach ($aSomeArray as $aSomeArrayKey => $aSomeArrayValue)
{
echo "<td>".$aSomeArrayValue."</td>";
if($aSomeArrayValue % 4 == 0)
echo "</tr><tr>";
}
echo "</tr>";
echo "</table>";
Try this code
<?php
$i=0;
echo"<table>";
echo"<tr>";
foreach($aSomeArrayas as $val)
{
if($i %4 ==0)
{
echo"</tr><tr> <td> $val</td>";
}
else
{
echo"<td> $val </td>";
}
$i++;
}
echo"</tr>";
echo"</table>";
?>