i am trying to get my list of tags but using a foreach so that i can put each tag into a list element.
What i used to do was:
$tagsArray = "these,are,some,tags"
// Explode the tags
$tagsArray = explode(",",$tags);
<ul>
<?php
// Create a list for the tags
foreach($tagsArray as $var)
{
echo "<li>$var</li>";
}
?>
</ul>
However, i now need to display this array inside an echo as well as other items.
Example:
<?php
echo "
<div id='editInfomation'>
<div id='Title'>This is a title</div>
<div id='tags'>
<ul>$tags</ul>
</div>
</div>
";
?>
How should i create a $tags variable that will contain all the exploded tags but in list element tags? Thanks.
This is what you need.
$tagsArray = "these,are,some,tags";
$tags = "<li>".implode("</li>,<li>",explode(",",$tagsArray ))."</li>";
And you simply print
<?php
echo "
<div id='editInfomation'>
<div id='Title'>This is a title</div>
<div id='tags'>
<ul>$tags</ul>
</div>
</div>
";
?>
I'm not 100% sure on what you're asking, but:
How should i create a $tags variable that will contain all the exploded tags but in list element tags?
You can push each element into an array, formatted:
$output = array();
foreach($tagsArray as $var) {
$output[] = "<li>$var</li>";
}
... then:
echo "<ul>" . implode($output) . "</ul>";
Related
I have the following data and would like to display it in different containers in html.
Name Price Difference Signal
CA.PA 15.85 3.5609257364073 MACD
AZN.ST 896 3.4881049471963 MACD
AMGN 258.57 1.6391533819031 SMA 50/200
The containers are winner_1. As of right now the first winner_1 display the last Name from the above table.
How can I get it to say CA.PA in the first winner_1, and AZN.ST in the second winner_1, and AMGN in the last winner_1.
<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'];
}
?>
<div class="winner_1">
<?php echo $name; ?>
</div>
<div class="winner_1">
<?php echo $name +1; ?>
</div>
<div class="winner_1">
</div>
</div>
</div>
The page can be seen here:
https://signal-invest.com/markets-today/
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>
You should see following logic and try doing this way. Hopefully your problem will be resolved.
<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'><a href='#'> $name </a></div>";
}
?>
</div>
</div>
I'm new using JSON and PHP.
I have a big list of products in JSON that is being decoded through PHP and I need this list to be splited in 3 columns. How can I do this?
Here is the code I'm using now:
<div class="col-md-4">
<div class="section-title">
<ul class="dados">
<?php
$url = 'http://myurl/consult';
$data = file_get_contents($url);
$characters = json_decode($data);
foreach ($characters as $character) {
?>
<li class="nomes-ies"><?php echo $character->nome . '</li>'; } ?>
</ul>
</div>
</div>
You could use array_chunk() to split your array in three elements. Then, use a foreach on this array, around the <div class="col-md-4">:
<?php
$url = 'http://myurl/consult';
$data = file_get_contents($url);
$characters = json_decode($data);
$cols = array_chunk($characters, ceil(count($characters) / 3));
foreach ($cols as $data) {
?>
<div class="col-md-4">
<div class="section-title">
<ul class="dados">
<?php foreach ($data as $character) { ?>
<li class="nomes-ies"><?php echo $character->nome ?></li>
<?php } ?>
</ul>
</div>
</div>
<?php } // end foreach $cols ?>
Note that the number of elements used in array_chunk() must be computed with ceil() to avoid to get 4 columns. The last column could have less elements than the other.
I get the result from my database i this type of array format.Now i want to print the question,answer and category name from the array what I do?
<?php $v1 = '';
foreach($var as $data){
?>
<div class="faqHeader"> <?php echo $data['category_name'];?> </div>
<div class="panel-group" id="<?php echo $data->title;?>">
I tried these cod but not get the answer.In $var i get the all the array value
Things to consider:-
1.Your sub-array is again an array so you need to use foreach() on sub-array too. (So basically two foreach())
2.You need to close divs as well as foreach() loops too.
So code need to be like below:-
<?php
foreach($var as $data){
foreach($data['data'] as $dat){
?>
<div class="faqHeader"> <?php echo $dat['category_name'];?>
<div class="panel-group" id="<?php echo $dat['questions'];?>"><?php echo $dat['questions'];?></div>
<div class="panel-group" id="<?php echo $dat['answer'];?>"><?php echo $dat['answer'];?></div>
</div>
<?php } }?>
you can use two loops to simplify your task:-
foreach($var as $index=>data){
echo $data['name']; // this will be your category name
foreach($data as $questionIndex=>$questionData){
echo $questionData['question'];
echo $questionData['answer'];
}
}
Since your array contains multiple data and data contains multiple values itself, you need two foreaches here:
foreach($var as $item) {
foreach($item['data'] as $info) {
var_dump($info);
}
}
Please try this code
<?php
$v1 = '';
foreach($var as $data){
?>
<div class="faqHeader"> <?php echo $data['data'][0]['category_name'];?> </div>
<div class="panel-group" id="<?php echo $data['name'];?>">
<?php
}
?>
You don't have any 'title' index in your array so you may change it to 'name'
Hi guys this seems to be a very basic question,
I have an array of categories, and would like to display each one 4 times.they are all floating left, so each category is displayed as a row of 4 boxes.
however there are some divs which are boxes with border that are displayed with each iteration. It seems to be working but i dont know if it's the correct way to do it, as all the
categories are not organised. I want a header of the specific category to appear first thing each time the loop jumps to the next item in array;
Any help is welcome. please see code below.
[CODE]
<section class="prodList">
<?php
$loop = 4;
$prodNum = 0;
$categories = array("All","Clothes","Gadgets","Games");
foreach($categories as $cat){
echo "<h2> new cat: ".$cat."</h2>";
while($prodNum < count($categories)*$loop){
echo "<div class='viewerWishlistProdContainer'>
<div class='prodImageContainer'>
</div>
</div>
";
echo " <p>
<button class='sortBtns' onClick='alert(\'See all V'\)'>See all V</button>
</p>";
$prodNum ++;
}
}
[/CODE]
Would this work?
<section class="prodList">
<?php
$categories = array("All","Clothes","Gadgets","Games");
foreach($categories as $cat){
/** Since you know it should repeat four times, why not echo 4 times? */
echo "<h2> new cat: ".$cat."</h2>";
echo "<div style='display:inline-block;padding:5px'>". $cat . "</div>";
echo "<div style='display:inline-block;padding:5px'>". $cat . "</div>";
echo "<div style='display:inline-block;padding:5px'>". $cat . "</div>";
?>
<!-- You don't have to echo plain HTML -->
<div class='viewerWishlistProdContainer'>
<div class='prodImageContainer'>
</div>
</div>
<p>
<button class='sortBtns' onClick='alert(\'See all V'\)'>See all V</button>
</p>
<?php } /** closing the foreach block */ ?>
</section>
i have a website structure as follow
<div id="title">
<h1> //call $title here after executing loop </h1>
</div>
<?php
...
while ($row = $result->fetch_assoc()) { $title = $row['title']; ?>
<h2> this is the <?php echo $title;?> </h2>
<?php } ?>
is there a way i could still use or call the $title variable on an html element on top of the while loop statement?
whenever i call it above the while loop i get errors like Undefined variable: id..
edit: change id into 'title' instead
PHP will read code line by line so variable can't be used before declaration.
Only one option is to move loop above your div, get html from loop inside variable and print it later.
I think that you've got only one row in that loop so use this code instead.
$row = $result->fetch_assoc();
$title = $row['title'];
echo '<h2> this is the '.$title.' </h2>';
If you're only retrieving a single row, you don't need the loop:
<?php
$row = $result->fetch_assoc();
$title = $row['title'];
?>
<div id="title">
<h1><?php echo $title; ?></h1>
</div>