Merge <tr> <td> in php foreach [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a dynamic table where a foreach loop generate every tr and the first td values
My problem is that every tr has 2nd 3rd 4th and 5th td column. These are also generated by a new foreach.
What is the smartest way to synchronise these loops to show data correctly?

I think this is very simple:
<?php
$rows = '5'; // Number of rows that you want
echo "<table>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
echo "<td>{$tr}*1</td>";
for($td=2;$td<=5;$td++){
echo "<td>{$tr}*{$td}</td>";
}
echo "</tr>";
}
echo "</table>";
?>

Related

Hard understanding 'echo' from associative array structure [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
So, I get this assoc array structure :
pastebin.com/9nEGKsK0
How can I fetch the urls by 'foreach'?
Help.
You can try this:
<?php foreach($array['response']['items'] as $item) {
foreach($item['sizes'] as $size) {
echo $size['url'];
}
}
echo '<pre>';
print_r($array);
echo '</pre>';

foreach loop in Multidimensional Arrays [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
$marks=array(
array(a1,a2,a3),
array(b1,b2,b3),
array(c1,c2,c3)
);
so i want this in foreach loop
example
echo "text-start".$marks[0]."text11".$marks[1]."text22".$marks[2]."text-end";
and result to be:
text-starta1text11a2text22a3text-end
text-startb1text11b2text22b3text-end
text-startc1text11c2text22c3text-end
i tried this but it show a1a2a3 b1b2b3
and i cant add text between them
for($r=0;$r<count($marks);$r++)
{
for($c=0;$c<count($marks[$r]);$c++)
{
echo $marks[$r][$c]."test";
}
echo "111<br>";
}
Basics...
foreach($marks as $marky_marks){
echo "text-start".$marky_marks[0]."text11".$marky_marks[1]."text22".$marky_marks[2]."text-end";
}

Undefined offset: 2 ( php function ) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
So-.. I was looking up how to fix this function; but I can't seem to find out. Can anyone give a hand?
I get an error on this line ( I'll include the php function ).
PHP function:
while($sql_row = mysqli_fetch_row($result))
Actual faulty line:
<tr>
<td><a <?php echo 'href="character_staff.php?action=manage&name=' . $sql_row[0] . '"'; ?>><?php echo $sql_row[0]; ?></a></td>
<td><a <?php echo 'href="character_staff.php?action=manage&name=' . $sql_row[0] . '"'; ?>><?php echo $sql_row[2]; ?></a></td>
</tr>
I can't seem to find out how to fix it. It has to change the URL into a link that contains text ( IE: character_staff.php?action=manage&name=account_name )
The problem is here:
<?php echo $sql_row[2]; ?>
Your code is attempting to echo the third element of the $sql_row array, but the array does not contain three elements. There are two ways to fix the problem: change the code to reference only elements that exist in the array, or change the array to contain more elements.
Note: in case you are wondering why $sql_row[2] looks for the third element, it is because arrays are zero-indexed.
$sql_row[0]
returns the first element.
$sql_row[1]
returns the second element, and so on.

PHP: easy random picture code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My code is random but its does not print out the picture just the text. When I add the IMAGE tag. Its go all wrong. Where do I go wrong?
$plaatje[] = 'afbeelding1.jpg';
$plaatje[] = 'afbeelding2.jpg';
$plaatje[] = 'afbeelding3.jpg';
$plaatje[] = 'afbeelding4.jpg';
$nummer2 = mt_rand(1,4);
echo "$plaatje[$nummer2]";
Use array_rand() - Picks one or more random entries out of an array
echo $plaatje[array_rand($plaatje)];
With an image tag, something like:
<img src="<?php echo $plaatje[array_rand($plaatje)]; ?>" />
or
echo "<img src='".$plaatje[array_rand($plaatje)]."' />";
Here is the documentation
Although you should add the image tag to show where it goes wrong, your code already has a problem as array indices are 0-based.
So you would need:
$nummer2 = mt_rand(0,3);

Converting php number value to words with colour [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I am unsure what I am to search for this question so not much research has been done.
I have a MySQL table that I store scores for a pool team as 1 for a win and 0 for a loss
i am displaying the values on a table but would like to change the output into words maybe different colours depending on the value.
the value 1 i like to read Win in green and value 2 to read lose in red.
how can i do this? or can u link me to a basic tutorial
Perhaps you need a conditional operator:
http://davidwalsh.name/php-shorthand-if-else-ternary-operators
$output="Result:".( $score ? "<font color=green>$score</font>":"<font color=red>$score</font>");
You should really make an attempt before asking a question on here. Giving you the benefit of the doubt, I'd say look into enumerations (since you seem to not want to use a simple conditional for some reason)
You simply can put a condition in the table, to check the score value. Supposing you have an array of scores:
<table>
<? foreach($score in $scores) ?>
<tr>
<td>some data</td>
<td>some data</td>
...
<td>
<?
if($score == 1) { echo "Win" } else { echo "Lose" }
?>
</td>
</tr>
</table>
There been a lot of great answers on this question of mine but if anyone looking for a quick fix in this case the following works for me
<?php
if ($fs1 > 0) {
echo "<p style='color:#0F3'>Win<p>";
} else {
echo "<p style='color:#F00'>Lose<p>";
}
?>
with $fs1 being the row that i am getting the value from
Without the links within the answeres i would never of found out how to do it

Categories