echo "<table>";
for ($x=0; $x<=count
($arr['chart_data']); $x++) {
echo "<tr>\n";
foreach($arr['chart_data'][$x] as $key=>$val)
{
echo "<td align='center;' style='color:white;'>". $val . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
I want to show only the first 4 fields in the table. I am decoding a multidimensional array in php. I
Try like
$cnt = 0;
foreach($arr['chart_data'][$x] as $key=>$val) {
if($cnt++ < 4) {
echo "<td align='center;' style='color:white;'>". $val . "</td>\n";
} else {
break;
}
}
Related
I want to create a game where I have to create a map with PHP. I want to create 100 tables boxes in the right way but I can't get it work...
$field = 100;
echo "<table border='3px' dir='ltr'>";
for ($row=0; $row < 10 ; $row++) {
echo "<tr>";
for ($column=0; $column < 10; $column++) {
echo "<td>";
echo $field;
$field--;
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
This gives me this table:
But I will need a table like:
If a for loop is not a requirement.
I would use an array, then you have a set to work from rather then calculating things, then you just need reverse the odd row, like so:
$rows = array_reverse(array_chunk(range(1, 100), 10));
echo "<table>\n";
foreach ($rows as $level => $row) {
if (($level-1) % 2) {
$row = array_reverse($row);
}
echo "\t<tr>\n";
foreach ($row as $value) {
echo "\t\t<td>$value</td>\n";
}
echo "\t</tr>\n";
}
echo "<table>\n";
https://3v4l.org/204eh
the roblem is where there is the punch commentator
i need when the column has INTER name or OUTER name to format the content of the $cell
with $cell=number_format($cell,2,',','.')
i'm just starting using php so don be too specific thanks
<?php
// printing table rows
$rigapadi = 1 ;
while($row = mysql_fetch_row($result))
{
echo "<tr>";
$rigapadi=$rigapadi+1;
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
if ($rigapadi % 2 == 0) {
# if column name = 'INTER' or 'OUTER' $cell = number_format($cell, 2, ',', '.');
echo "<td align=\"center\">$cell</td>";
} else {
echo "<td bgcolor=\"#E9EEF5\" align=\"center\">$cell</td>";
}
echo "</tr>\n";
}
mysql_free_result($result);
echo "</table>";
echo "<p/>";
?>
The mysql_ are obsoltes. You must not use them. And please indent your code properly when you ask for help.
<?php
$rigapadi = 1;
while($row = mysql_fetch_assoc($result)) {
echo '<tr>';
$rigapadi++;
foreach($row as $name => $cell) {
if($rigapadi % 2 === 0) {
if($name === 'INTER' || $name === 'OUTER') {
echo '<td align="center">' . number_format($cell, 2, ',', '.') . '</td>';
} else {
echo '<td align="center">' . $cell . '</td>';
}
} else {
echo '<td align="center" bgcolor="#E9EEF5">' . $cell . '</td>';
}
}
echo "</tr>\n";
}
mysql_free_result($result);
echo "</table>";
?>
Note that I use fetch_assoc to get the $name.
PS: <p/> does not exists and align and bgcolor and not valids but acceptable if this is a HTML code for an e-mail.
I have a foreach function which permits me to print fields in a database,
I want to do possible that if $row[value]="photo" don't just print the $row[value] but another thing.
Can I do it?
The code is this:
while($row = mysql_fetch_array($result)) {
echo "<tr>";
foreach($checked1 as $key => $value){
echo "<td>" . $row[$value] . "</td>";
}
I need to have a condition that if $row[value]=photo to echo another thing not: echo "<td>" . $row[$photo] . "</td>";
Any example?
Since i could not get what to print for if($row['value'] == photo) check, so add that code yourself in the code written below.
while($row = mysql_fetch_array($result)){
echo "<tr>";
foreach($checked1 as $key => $value){
if($value == 'photo'){
echo "<td><img src = '".$row[$value]."'></td>";
}else{
echo "<td>" . $row[$value] . "</td>";
}
}
}
This doesn't need to be complex.
foreach ($result as $row) {
if ($row['value'] === 'photo') {
// print "another thing"
} else {
// print "something"
}
}
Sure, just include an if statement with that. Your code will be similar to the code below:
$arr = array("one", "two", "three");
foreach ($arr as $value) {
if($value == "one")
echo "Not gonna print one.";
else
echo "Value: $value<br />\n";
}
Using your code, you can see if the value is not photo, echo it.
while($row = mysql_fetch_array($result)) {
echo "<tr>";
foreach($checked1 as $key => $value){
if($row[value] == "photo") {
echo "some other thing";
} else {
echo "<td>" . $row[$value] . "</td>";
}
}
}
If you want to print img src if the field is "photo" field (key) then
Try this :
while($row = mysql_fetch_array($result)){
echo "<tr>";
foreach($checked1 as $key => $value){
if($key == 'photo'){
echo "<td><img src = '".$row[$value]."'></td>";
}else{
echo "<td>" . $row[$value] . "</td>";
}
}
}
Hello i'm having problems adding numbered rows/serial numbers to my database query result. I've used $number to collect the actual number of rows.
Then another problem i'm trying to avoid is: Numbering of Column Headers.
Thanks for the help.
<?php
$number = mysql_num_rows($query);
for ($serial = 0; $serial < $number; $serial++)
{
echo "<tr>". $serial ."</tr>";
}
for ($i = 0; $i < $number_cols; $i++)
{
echo "<th>" . mysql_field_name($query, $i) . "</th>\n";
}
while ($row = mysql_fetch_row($query))
{
echo "<tr align=center>\n";
for ($i = 0; $i < $number_cols; $i++)
{
echo "<td>";
if (!isset($row[$i]))
{
echo "NULL";
}
else
{
echo $row[$i];
}
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>";
echo "</span>";
echo "</div>";
?>
trying my best to get something sane out of your terrific code.
<?php
$serial = 1;
while ($row = mysql_fetch_row($query))
{
echo "<tr align=center>\n";
echo "<td>";
echo $serial++;
echo "</td>\n";
foreach ($row as $value)
{
echo "<td>$value</td>\n";
}
echo "</tr>\n";
}
Here is my code so far now i want to combine this what i can do ?
<table>
<tr>
<th>Imported Files</th>
<th>Report Files</th>
</tr>
<?php
$dir = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/u*.[cC][sS][vV]");
$dir2 = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/r*.[cC][sS][vV]");
foreach(glob($dir) as $file) {
echo "<tr>";
echo "<td>". $file."</td>";
}
foreach(glob($dir2) as $file) {
echo "<td>". $file."</td>";
}
echo "</tr>";
?>
</table>
I want to print like this
echo "<tr>";
echo "<td>". $file1."</td>";
echo "<td>". $file2."</td>";
}
echo "</tr>";
i.e in the same td what I can do Help me
update:-
I want to print this in to td
$dir = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/u*.[cC][sS][vV]");
$dir2 = str_replace("/var/www/13_Feb/subscriber-files/","","/var/www/13_Feb/subscriber-files/r*.[cC][sS][vV]");
This one handles files lists of different sizes.
Plus, you can have more than two columns of "file lists" displayed in the same table.
// retrieve data
$dirs = array(glob($dir), glob($dir2));
// display
$max_files = max(array_map('count', $dirs));
$count_dirs = count($dirs);
echo '<table>';
for($i = 0; $i < $max_files; $i++)
{
echo '<tr>';
for($d = 0; $d < $count_dirs; $d++)
{
$strFile = (isset($dirs[$d][$i]) ? $dirs[$d][$i] : '[NO FILE]');
echo '<td>'.$strFile.'</td>';
}
echo '</tr>';
}
echo '</table>';
$line .= "<tr>";
foreach(glob($dir) as $file) {
$line .= "<td>". $file."</td>";
}
foreach(glob($dir2) as $file) {
$line .= "<td>". $file."</td>";
}
$line .= "</tr>";
echo $line;
If they should be combined and both are of the same size, perhaps this will be a solution (untested)
$dir1_files = glob($dir);
$dir2_files = glob($dir2);
for ($i =0; $i < count($dir1_files); $i++)
{
echo "<tr>";
printf("<td>%s</td>", $dir1_files[$i]);
printf("<td>%s</td>", $dir2_files[$i]);
echo "</tr>";
}
PHP 5.3+ has a MultipleIterator for this, combined with the GlobIterator you can have a solution like this:
$iterator = new MultipleIterator();
$iterator->attachIterator(new GlobIterator($dir);
$iterator->attachIterator(new GlobIterator($dir2);
foreach ($iterator as $current) {
echo "<tr>";
echo "<td>". $current[0] ."</td>";
echo "<td>". $current[1] ."</td>";
echo "</tr>";
}
If you have a different number of files and want to display all of them, you can use the MultipleIterator::MIT_NEED_ANY Flag:
$iterator = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
$iterator->attachIterator(new GlobIterator($dir);
$iterator->attachIterator(new GlobIterator($dir2);
foreach ($iterator as $current) {
echo "<tr>";
echo "<td>". (isset($current[0]) ? $current[0] : ' ') ."</td>";
echo "<td>". (isset($current[1]) ? $current[1] : ' ') ."</td>";
echo "</tr>";
}