PHP Simple Foreach Loop with HTML

PHP simple foreach loop with HTML

This will work although when embedding PHP in HTML it is better practice to use the following form:

<table>
<?php foreach($array as $key=>$value): ?>
<tr>
<td><?= $key; ?></td>
</tr>
<?php endforeach; ?>
</table>

You can find the doc for the alternative syntax on PHP.net

PHP HTML foreach loop

I have fixed my code a bit and this is what worked out:

$baseConf = array(
'site' => array("abc.com","abc.org"),
);
?>
<!DOCTYPE html>
<html>
<head>
<title>Default Page</title>
</head>
<body>
<!--<a href="<?php //$site; ?>"><?php //$site; ?></a>-->
<?php
foreach ($baseConf as $value) {
//echo "$value\n";
print_r($value);
}
?>
</body>
</html>

Foreach PHP loop with HTML code

It looks like you have some syntax errors with the quotation marks. Try concatenating the variables:

echo "<option value='Hardware" . $GLOBALS['filters_set']['trdfcat']['selected'][$category] . "'>$category</option>";

If you really want to interpolate:

echo "<option value='Hardware{$GLOBALS['filters_set']['trdfcat']['selected'][$category]}'>$category</option>";

You can also split things up if it makes it easier:

echo "<option value='Hardware";
echo $GLOBALS['filters_set']['trdfcat']['selected'][$category];
echo "'>$category</option>";

Html within Php foreach loop

You need to first build your string with desired outout. Then return that string

<?php
class Dummy
{
public static function testing($data)
{

$str = '<div class = "dummy">name:' . $data['name'] . '</div>
<table>';

foreach($data as $d){
$str .= '<tr><td>hello</td></tr>';
}
$str .= '</table>';
return $str;

}
}
?>

foreach loop for creating a HTML markup from a PHP array

You can do it with stack,

$array = array(
1=>array( 'level' => 1, 'element' => '<div class="parent">'),
2=>array( 'level' => 2, 'element' => '<div class="child">'),
3=>array( 'level' => 3, 'element' => '<span class="child2">'),
4=>array( 'level' => 2, 'element' => '<div class="child">'),
5=>array( 'level' => 2, 'element' => '<div class="child">'),
6=>array( 'level' => 3, 'element' => '<span class="child2">'),
7=>array( 'level' => 4, 'element' => '<span class="child3">'),
);

$result = "";
$tags = []; // stack to store node end tag
$levels = []; // stack to store node level
foreach($array as $tag){

$level = $tag["level"];
$element = $tag["element"];

while(end($levels) >= $level){ // pop all Sibling and their child
array_pop($levels);
$result .= array_pop($tags);
}

$result .= str_pad("",$level-1,"\t") . $element . "\n";
array_push($tags, str_pad("",$level-1,"\t") . "</" .substr($element,1,strpos($element," ")-1) . ">\n");
array_push($levels,$level);
}
while(end($levels)){
array_pop($levels);
$result .= array_pop($tags);
}
echo $result;

And the result,

php test.php
<div class="parent">
<div class="child">
<span class="child2">
</span>
</div>
<div class="child">
</div>
<div class="child">
<span class="child2">
<span class="child3">
</span>
</span>
</div>
</div>

PHP foreach in html tags

You had a space between "<" and php keywords in your foreach, also you didn't have opening bracket or colon on the end of the same line. Try something like:

<?php foreach($rows as $value): ?>
....
<?php endforeach; ?>

Use PHP foreach loop to display data in many html div tags

One option is generated div tags using php:

<div class="overview">
<h1>Winners</h1>
<div class="winner">
<?php
foreach ($res_winners_weekly as $r) {
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];

echo "<div class='winner_1'>";
echo "<a href='#'>{$name}</a>";
echo "</div>";
}
?>
</div>
</div>

Create Html table in php foreach loop

End of your table is inside loop. make table header and footer outside loop.

<?php
global $wpdb, $indeed_db;
$user = wp_get_current_user();
$userid = $user->ID; ?>
<table>
<thead>
<tr>
<th>Order ID</th>
<th>My ID</th>
<th>Create Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$woo_orders = $wpdb->get_results("SELECT * FROM wp8u_wc_order_product_lookup"); foreach ($woo_orders as $print ){ $order_id = $print->order_id; $woo_customer_id = $print->customer_id; $woo_customer = $wpdb->get_results("SELECT * FROM
wp8u_wc_customer_lookup where customer_id=$woo_customer_id"); foreach($woo_customer as $print2){ $current_user_uid = $print2->user_id; } $date1 = strtotime($print->date_created); $date_created = date('Y-m-d H:i:s', $date1); $amount =
$print->product_net_revenue; if($userid == $current_user_uid){ ?>
<tr>
<td><?php echo "$order_id";?></td>
<td><?php echo "$woo_customer_id";?></td>
<td><?php echo "$date_created";?></td>
<td><?php echo "$amount";?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>


Related Topics



Leave a reply



Submit