<?php
$i=0;
while($i < 101){
if($i%2==0){
echo "<tr>".PHP_EOL;
}
echo "<td>".$i."</td>".PHP_EOL;
$i++;
if($i%2==0){
echo "</tr>".PHP_EOL;
}
}
?>
This code generates a table with 100 rows and 2 columns. But what I want to do is that show ordered numbers (upp to 100) in the left side of the rowcells and show something else (ex. pow(rownumber) ) in the right side of the rowcells. How can I do that?
Try this, Will output 100 rows with the number and its power in two columns
<table>
<?php
for($i = 0; $i <= 100; $i++){
echo sprintf('<tr><td>%s</td><td>%s</td></tr>',
$i,
pow($i, 2)
);
}
?>
</table>
Is this what you are looking for?
for($i=0; i<100; i++){
if($i%2==0){
echo "<tr>".PHP_EOL;
}
echo "<td>".$i."</td>".PHP_EOL;
echo "<td>Something Else</td>".PHP_EOL;
if($i%2==0){
echo "</tr>".PHP_EOL;
}
}
You can use your modulus (and a for loop too)
for ($i = 1; $i <= 100; $i++){
$mod = ($i%2==0) ? true : false;
if($mod) echo "<tr>".PHP_EOL;
echo "<td>".$i."</td>".PHP_EOL;
echo "<td>". foo($bar) ."</td>".PHP_EOL;
if($mod) echo "</tr>".PHP_EOL;
}
<?php
$i=0;
$j=0;
while($i < 101){
if($i%2==0){
$j++;
echo "<tr>"."<td>".$j."</td>".PHP_EOL;
}
echo "<td>".$i."</td>".PHP_EOL;
$i++;
if($i%2==0){
echo "<td>any text</td>"."</tr>".PHP_EOL;
}
}
?>
Why use modulo for only two options? This solution seems much easier. Your $data is an array of the things you want to display, currently the alphabet.
$data = range('a','z');
foreach($data as $num => $elem) {
echo "<tr>".PHP_EOL;
echo "<td>".$num.</td>".PHP_EOL;
echo "<td>".$elem.</td>".PHP_EOL;
echo "</tr>".PHP_EOL;
}
If you want to make it loop 100+ times, just make the array that size.
Foreach documentation
Related
I want to print the values of variable but it prints 12 only
$str1="abhishek";
$str2="ashish";
for($i=1;$i<=2;$i++)
{
echo $str.$i;
}
This is a better practice:
<?php
$str = array("abhishek","ashish");
for($i=0; $i <= 2; $i++)
{
echo $str[$i].'<br>';
}
?>
Do this:
$str1="abhishek";
$str2="ashish";
for($i=1;$i<=2;$i++){
echo ${"str".$i};
}
I want to make in html page like this below:
But, I don't want to write them all one by one, because the number(1, 2, 3) are from $number variable and it can be more than 3.
Use a for loop:
for ($i = 1; $i <= $number; $i++) {
echo "<a href='$i'";
if ($i == $currentvid) {
echo " class='currentvid'";
}
echo "></a>";
}
Try this:
<?php
$href="1";
for($i=1;$i<4;$i++){
if($href==$i){
echo "$i";
}else{
echo "$i";
}
}
?>
$i = 1;
foreach ($number as $k)
{
if($i == 1)
{
echo "$i";
}
else
{
echo "$i";
}
}
}
I'm trying to output my array results in groups of 4.
<?php for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } ?>
The above does 4, but obviously doesn't re-loop.
You can loop whole array and group you output with help of "%" operator.
<div>
<?php for ($i = 0; $i < count($array); $i++) {
if (($i % 4) == 0) {
echo "</div><div>";
}
echo "Element " . $array[$i]; // CODE
}
</div>
Other than using Mod as the other answers show, you could use array_chunk() to create the groups:
$groups = array_chunk($original_array, 4);
foreach($groups as $group){
echo '<div>';
foreach($group as $item){
echo $item;
}
echo '</div>';
}
You can use a while loop to reloop for the whole results to be printed
<?php while(conditions) {
for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } } ?>
Try this that way you can jump by 4
for ($i = 0; $i < 20; $i = $i+4) {
echo $i.'<br/>';
}
I would use a foreach and then just throw in an extra check to output the divs.
$i=0;
foreach ($array as $key->$val)
{
if($i%3==0)
{
echo "<div>";
}
// your stuff
if($i%3==0)
{
echo "</div>";
}
$i++;
}
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
you can check out from here http://php.net/manual/en/function.array-slice.php
try this, use nested for loop, this will loop 4 times. You can try to integrate with your code. If
for ($i = 0; $i < 4; $i++){
for($j = 0; $j < 4; $j++){
echo $a[$j++];
}
echo "<br/>";
}
I hope it can help you.
you can try $i++, because you use ++$i in this way "for" works 3 times!
for ($i = 0; $i < 4; $i++)
Here's my code:
echo "<table><tr>";
$count = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
extract($row);
$data = $row['info'];
echo "<td>".$data."</td>";
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
}
echo "</tr></table>";
The above code works as:
=========
= 1 = 2 =
= 3 = 4 =
= 5 = 6 =
=========
But how do I display the info in this format?
=========
= 1 = 4 =
= 2 = 5 =
= 3 = 6 =
=========
Actually, you don't need to parse array to another form...
<?php
$a = array(1,2,3,4,5,6,7);
$c = ceil(count($a)/2);
echo "<table>";
for($i=0; $i<$c;++$i){
echo "<tr><td>{$a[$i]}</td><td>{$a[$i+$c]}</td></tr>";
}
echo "</table>";
?>
Ofcourse you need to modify this code by adding db operations (like mysql_num_rows instead of count) but it works fine: enter link description here
Thanks, guys! I appreciate all your help, but this code works perfect for me. I just want to share it with others who may find this helpful. :)
$tmp=array();
$columns=2;
$row=ceil(mysql_num_rows($result)/$columns);
for ($x =1; $x <= $columns; $x++)
for ($y = 1; $y <= $row; $y++)
$tmp[$x][$y]=mysql_fetch_array($result);
echo "<table align=center width=\"50%\">\n";
for ($y =1; $y <= $row; $y++) {
echo "<tr>";
for ($x = 1; $x <= $columns; $x++)
if (isset($tmp[$x][$y]['ID']))
echo "<td>".$tmp[$x][$y]['info']." </a></td>";
else
echo "<td></td>";
echo "</tr>\n";
}
echo "</table>\n";
A brief PSA... the mysql_ extension is depreciated and will eventually be removed. You need to start using mysqli_ now.
You need to parse your data first. Once you've done that, generating the table is easy. This should work for all data sets
echo "<table>";
$count = 1;
$col1 = $col2 = array();
$rowcount = round(mysql_num_rows($result) / 2);
while($row = mysql_fetch_assoc($result)) {
if($count > $rowcount) $col2[] = $row['info'];
else $col1[] = $row['info'];
$count++;
}
$counter = 0; // Arrays start with 0
foreach($col1 as $row) { // $col1 will always be >= $col2
$row2 = (isset($col2[$counter])) ? $col2[$counter] : "";
echo "<tr><td>" . $row . "</td><td>" . $row2 . "</td></tr>";
$counter++;
}
echo "</table>";
This is untested, but you can acheive this cleanly with 2 loops. Build up the left and right columns based on the the $count variable. Build the HTML all the way up, then echo it.
Edit: I didn't realize you were dividing the data in half and placing the first half on the left column of the table. I have changed the code below to do this.
<?php
$left = array();
$right = array();
$count = 1;
$num_rows = mysql_num_rows($result);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($count <= $num_rows/2) {
array_push($left, $row['info']);
} else {
array_push($right, $row['info']);
}
$count++;
}
$html = "<table>";
for ($i = 0; $i < $count; $i++) {
$html .= "<tr><td>"
. htmlentities($left[$i])
. "</td><td>"
. htmlentities($right[$i])
. "</td></tr>";
}
$html .= "</table>";
echo $html;
?>
Here is some php magic that could shorten that code for a bit
<?php
$array = range(1, 20);
$count = 2;
$out = array_chunk($array, ceil(count($array)/$count));
array_unshift($out, null);
$out = call_user_func_array("array_map", $out);
?>
<table>
<?php foreach ($out as $row) : ?>
<tr>
<?php foreach($row as $column) : ?>
<td><?php echo( $column ); ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
Also, i would recommend you switch your database-related code to PDO. Besides all those nice features, like auto-escaping, you can easily get an array of elements from your DB query, without all this mysql_fetch_array nonsense.
I new to PHP. I'm trying to make an Incidence of Coincidence (IC) calculator from bits and pieces of code that i've patched together from other peoples work. It's all working fine except for the part where it is posted to table. I want it to stop making new cells after 50.
I've been looking at it for hours and have tried several other methods but i've confused myself. Any help please?
Here's the code that's writes the table:
<?php
if ( $showTable ) {
// display tabulated values
echo "<table class=\"trans\" width=\"1000\" border=\"1\" cellpadding=\"0\">";
for ( $i = 1; $i < 50; $i++ ) {
echo "<tr>";
for ($c=0; $c <$cols; $c++)
{
echo "<td>";
echo($cells.': '.round($history[$i+$c], 3));
$cells++;
echo "</td>";
}
}
echo "</tr>";
$i += $c;
}
echo "</table>";
?>
If I understood you correctly,
<?php
if ( $showTable ) {
// display tabulated values
echo "<table class=\"trans\" width=\"1000\" border=\"1\" cellpadding=\"0\">";
for ( $i = 1; $i < 50; $i++ ) { // 50 rows will be created
echo "<tr>";
for ($c=0; $c <50; $c++) // each row will contain 50 <td>
{
echo "<td>";
echo($cells.': '.round($history[$i+$c], 3));
$cells++;
echo "</td>";
$i += $c;
}
echo "</tr>";
}
}
echo "</table>";
?>