I have an array here which has (30 values e.g.).And I displayed it on a div. I want to split them into 3parts and display them in a three separated div. How can I do this one?Here's my code:
<div class="span4">
<?php if($col):?>
<?php foreach($col as $names):?>
<label class="checkbox">
<input type="checkbox" id="chk_distinct" class="check"
value="<?php echo $names->COLUMN_NAME;?>">
<?php echo $names->COLUMN_NAME;?></label>
<?php endforeach;?>
<?php endif;?>
</div>
It will display this one:
<div>
Checkbox1
Checkbox2
Checkbox3
Checkbox4
...
Checkbox30
</div>
I wanted it to look like this one:
<div1> <div2> <div3>
Checkbox1 Checkbox11 Checkbox21
Checkbox2 Checkbox12 Checkbox22
Checkbox3 Checkbox13 Checkbox23
Checkbox4 Checkbox14 Checkbox24
Checkbox5 Checkbox15 Checkbox25
... ... ...
Checkbox10 Checkbox20 Checkbox30
</div> </div> </div>
Any Idea about this one?Beginner here..
Use array_chunk For example
$cols = array_chunk($col, 10, true);
foreach ($cols as $col)
{
echo '<div>';
foreach ($col as $names)
{
echo '<label>'.$names->COLUMN_NAME.'</label>';
}
echo '</div>';
}
Related
I am very new to PhP and programming in general, I looked at similar questions and tried most of the offered solutions but couldn't find a way to apply it to my situation.
I made a filter option on a page, I am now trying to have the filtered results come out as Ascending by price. If I am to use a sort function, where should I be using it in the code for it to make sense?
<?php
require "voitures.php"; [my array][1]
if (isset($_GET["prixMin"])) {
$prixMin = $_GET["prixMin"];
$prixMax = $_GET["prixMax"];
$voitures2 = [];
foreach ($voitures as $voiture) {
if ($prixMin <= $voiture['prix'] && $prixMax >= $voiture['prix']) {
$voitures2[] = $voiture;
}
}
}
?>
<form action=" <?= $_SERVER['PHP_SELF'] ?>" method="GET">
<label for="prixMin">Prix minimal : </label>
<input type="text" name="prixMin" value="<?php if(isset($_GET['prixMin'])){ echo $_GET['prixMin']; }?>">
<label for="prixMax">Prix maximal : </label>
<input type="text" name="prixMax" value="<?php if(isset($_GET['prixMax'])){ echo $_GET['prixMax']; }?>">
<input type="submit" value="Rechercher">
</form>
<br>
<div class="g" style="grid-template-columns: repeat(4, max-content)">
<div class="t">Marques</div>
<div class="t">Modeles</div>
<div class="t">Annee</div>
<div class="t">Prix</div>
<?php if (isset($_GET['prixMin'])) { ?>
<?php foreach ($voitures2 as $voiture2) : ?>
<div class="l">
<div class="c dr"><?= $voiture2["marque"] ?></div>
<div class="c"><?= $voiture2["modele"] ?></div>
<div class="c"><?= $voiture2["annee"] ?></div>
<div class="c mi"><?= $voiture2["prix"] ?></div>
</div>
<?php endforeach ?>
<?php } else { ?>
<?php foreach ($voitures as $voiture) : ?>
<div class="l">
<div class="c dr"><?= $voiture["marque"] ?></div>
<div class="c"><?= $voiture["modele"] ?></div>
<div class="c"><?= $voiture["annee"] ?></div>
<div class="c mi"><?= $voiture["prix"] ?></div>
</div>
<?php endforeach ?>
<?php } ?>
</div>
</main>
<footer>
</footer>
</body>
</html>
array_sort_by_column($voitures2,'prix',SORT_DESC)
function array_sort_by_column($array, $column, $direction = SORT_ASC) {
$reference_array = array();
foreach($array as $key => $row) {
$reference_array[$key] = $row[$column];
}
array_multisort($reference_array, $direction, $array);
return $array;
}
i have a veriable named (num) which is used for increments changes names of ids by 1 num+1 but in my foreachloop i cant access it .
i tried declaring it before the loop still doesnot work
<?php $num = 0; ?>
<?php foreach($listings as $list):?>
<li>
<div class="checkbox">
<input type="checkbox" class="css-checkbox" value="<?php echo $list['title'];?>" id="visit<?php echo $num+1;?>" name="treatment<?php echo $num+1;?>">
<label for="visit<?php echo $num+1;?>" class="css-label"><?php echo $list['title']?> <strong><?php echo $list['price'];?><span>£</span></strong></label>
</div>
</li>
<?php endforeach; ?>
</ul>
<hr>
<input type="hidden" value="<?php echo $num;?>" name="total"/>
i want the input ids to be incremented by 1 like treatment1,treatment2
You should increment the $num variable by doing $num++; once inside the loop, then print it where you need it with <?php echo $num; ?> without using <?php echo $num+1; ?> - as doing so will only increment it as you echo it - not add one to each iteration.
<?php
$num = 0;
foreach($listings as $list):
$num++; // Increment $num for each iteration
?>
<li>
<div class="checkbox">
<input type="checkbox" class="css-checkbox" value="<?php echo $list['title'];?>" id="visit<?php echo $num;?>" name="treatment<?php echo $num;?>">
<label for="visit<?php echo $num;?>" class="css-label"><?php echo $list['title']?> <strong><?php echo $list['price'];?><span>£</span></strong></label>
</div>
</li>
<?php endforeach; ?>
If your $listings is numeric indexed, you can use the key of each element in the array instead by doing
foreach($listings as $num=>$list):
?>
<li>
<div class="checkbox">
<input type="checkbox" class="css-checkbox" value="<?php echo $list['title'];?>" id="visit<?php echo $num;?>" name="treatment<?php echo $num;?>">
<label for="visit<?php echo $num;?>" class="css-label"><?php echo $list['title']?> <strong><?php echo $list['price'];?><span>£</span></strong></label>
</div>
</li>
<?php endforeach; ?>
The problem is you did not set the value of $num variable you just only print or echo it inside the html tags. You need to add or increment the $num variable inside the loop like this.
<?php $num++; ?>
or
<?php $num = $num+1; ?>
I'm trying to display text from a database row into 2 different divs.
The code currently selects all text from the row and displays both pieces of text in each div. Which isn't what I need. Is there a way to split the text, and have each text in its own div?
PHP:
//output each row
while ( $row = $result->fetch_assoc() ) {
echo $row["content"];
}
HTML:
//Text box 1
<div>
<p id="Text1">
<?php echo $row["content"]; ?>
</p>
</div>
//Text box 2
<div>
<p id="Text2">
<?php echo $row["content]; ?>
</p>
</div>
Thank you.
Just put the relevant HTML inside the loop:
$i = 1; // For the id's
while ( $row = $result->fetch_assoc() ) {
?>
<div>
<p id="Text<?= $i++; ?>">
<?= $row["content"]; ?>
</p>
</div>
<?php
}
//Do this
while ( $row = $result->fetch_assoc() ) {
$column1 .= '<p>'.$row["column1"].'</p>';
$column2 .= '<p>'.$row["column2"].'</p>';
$column3 .= '<p>'.$row["column3"].'</p>';
$column4 .= '<p>'.$row["column4"].'</p>';
//and so on...
}
//Column 1
<div id="column1">
<?php echo $column1; ?>
</div>
//Column 2
<div id="column2">
<?php echo $column2; ?>
</div>
//Column 3
<div id="column3">
<?php echo $column3; ?>
</div>
//Column 4
<div id="column4">
<?php echo $column4; ?>
</div>
//and so on...
I need to wrap every 2 divs with another div for a project (a row) so it will look like:
<div class="row">
<div> Item </div>
<div> Item </div>
</div>
<div class="row">
<div> Item </div>
<div> Item </div>
</div>
I have tried a few solutions but they dont work since the items coming in are odd (9 items). Here is what I had:
<?php
$count = 0;
foreach ($contents as $content)
{
//var_dump($content);
$books = $content["tags"];
$book_image = $content['content_image'];
$book_desc = $content['content_social_description'];
++$count;
if($count == 1)
{
echo "<div class='et_pb_row'>";
}
foreach ($books as $book)
{
$book_name = $book['tag_name'];
$book_name_trim = str_replace(' ', '-', $book_name);
?>
<!-- Inside the Book Loop -->
<div class='et_pb_column et_pb_column_1_2 books' style="background: url('https://s3-us-west-2.amazonaws.com/crowdhubproverbs31/<?php echo $book_image ;?>');">
<h2><?php echo $book_name; ?></h2>
<p><?php echo $book_desc; ?></p>
<?php echo $count; ?>
</div>
<?php
}
if ($count == 2)
{
echo "</div>";
$count = 0;
}
}
?>
This works except the second to last row has 3 items even though it dispays the "count" as 2 when I echo it out so it should reset but doesnt. So its:
<div class="row">
<div> Item </div> "Count 1"
<div> Item </div> "Count 2"
</div>
<div class="row">
<div> Item </div> "Count 1"
<div> Item </div> "Count 2"
</div>
<div class="row">
<div> Item </div> "Count 1"
<div> Item </div> "Count 2"
<div> Item </div> "Count 2"
</div>
<div class="row">
<div> Item </div> "Count 1"
<div> Item </div> "Count 2"
</div>
You should open and close your <rows/> in the books loop, and add a late check for odd books:
<?php
$count = 0;
foreach ($contents as $content)
{
//var_dump($content);
$books = $content["tags"];
$book_image = $content['content_image'];
$book_desc = $content['content_social_description'];
foreach ($books as $book)
{
++$count;
if($count == 1)
{
echo "<div class='et_pb_row'>";
}
$book_name = $book['tag_name'];
$book_name_trim = str_replace(' ', '-', $book_name);
?>
<!-- Inside the Book Loop -->
<div class='et_pb_column et_pb_column_1_2 books' style="background: url('https://s3-us-west-2.amazonaws.com/crowdhubproverbs31/<?php echo $book_image ;?>');">
<h2><?php echo $book_name; ?></h2>
<p><?php echo $book_desc; ?></p>
<?php echo $count; ?>
</div>
<?php
if ($count == 2)
{
echo "</div>";
$count = 0;
}
}
}
if ($count > 0)
{
echo "</div>";
}
?>
Doing so, your $count variable will be incremented only when foreach($books AS $book) loop is run (thus you have at least one book to print)
You can take advantage of array_chunk :
//First make a generator to get all books
function allBooks($contents) {
foreach($contents as $content) {
foreach($content['tags'] as $book) {
yield $book; //Here you can yield whatever you want !
}
}
}
//Then create rows
$itemPerRow = 2;
$rows = array_chunk(iterator_to_array(allBooks($contents)), $itemPerRow, true);
//Display all
foreach($rows as $row) {
echo '<row>';
foreach($row as $book) {
//Display the book !
}
echo '</row>';
}
How to use Foreach on two or more php arrays?
My arrays are $boardType, $hotelPrice and $hotelName
Solution is to add $index in the other arrays as suffix
<?php foreach ($hotelName as $index => $name):?>
<div>
<p><?php echo $name;?></p>
<p><?php echo $hotelPrice[$index];?></p>
<p><?php echo $boardType[$index];?></p>
</div>
<?php endforeach?>
This should work for you:
(Here I just go through all 3 arrays with array_map() an print them in the structure you want)
<?php
array_map(function($v1, $v2, $v3){
echo "<div>";
echo "<p>$v1</p>";
echo "<p>$v2</p>";
echo "<p>$v3</p>";
echo "</div>";
}, $boardType, $hotelPrice, $hotelName);
?>
Example input/output:
$boardType = [1,2,3];
$hotelPrice = [4,5,6];
$hotelName = [7,8,9];
<div>
<p>1</p>
<p>4</p>
<p>7</p>
</div>
<div>
<p>2</p>
<p>5</p>
<p>8</p>
</div>
<div>
<p>3</p>
<p>6</p>
<p>9</p>
</div>
If all arrays have exactly the same size you could use each to iterate through the other arrays at the same time as $hotelName:
<?php foreach ($hotelName as $index => $name):?>
$price = each($hotelPrice);
$boardType = each($boardType);
<div>
<p><?php echo $name;?></p>
</div>
<?php endforeach?>
However, in that case it would probably being better to have just a single array containing all the data.