Trying to sort my multi-dimensional array by price - php

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;
}

Related

Want to add a class to a list with specific ID using button

The final lists are looking like this and by clicking the green button I need to add class only to the list above. Only PHP is allowed
Click here
<ul class='wrapper'>
<?php foreach ($items as $item) : ?>
<li class="note" id='<?= $item['id'] ?>'>
<div class="details">
<p><?= $item['title']."<span class='currentDate'>".$item['currentDate']."</span>"?></p>
<p class ="importance"><?= $item['importance'] ?></p>
<div class="border"></div>
<span><?= $item['text'] ?></span>
</div>
<div class="bottom-content">
<span>Do: <?= date('d.m.Y', strtotime($item['date'])) ?></span>
<form method="post">
<button name="doneButton" value='<?= $item['id'] ?>' type="submit">✅</button>
</form>
<?php
if(isset($_POST['doneButton'])) {
//
//
}
?>
<form method="post" >
<button value="<?= $item['id'] ?>" name="deleteButton" type="submit">🗑️</button>
</form>
<?php
if(isset($_POST['deleteButton'])){
db_remove_note($_POST['deleteButton']);
header("Location: list-notes.php");
}
?>
</div>
</li>
<?php endforeach; ?>
</ul>
I want to add a class to li with the specific ID ($item['id']) using that doneButton. I need to use PHP only, with no JavaScript or MySQL.
Just add the condition to the li class attribute. I’d personally use a ternary operator as below to add MYNEWCLASS on the condition that $_POST['doneButton']) is set:
<li class="note<?=(isset($_POST['doneButton'])?" MYNEWCLASS":"");?>" id='<?= $item['id'] ?>'>
EDIT: To select an LI by a certain ID:
<?php
<ul class='wrapper'>
<?php foreach ($items as $item) : ?>
<?php
$class = ""; //Declare an empty $class
if (isset($_POST['doneButton']) && $_POST['doneButton'] == $item['id']){
// If $_POST['doneButton'] is set, and $_POST['doneButton'] is equal to the id of the current iteration then set $class to "MYNEWCLASS"
$class = " MYNEWCLASS"; //THE SPACE IS IMPORTANT
}
?>
<li class="note<?=$class;?>" id='<?= $item['id'] ?>'>
<div class="details">
<p><?= $item['title']."<span class='currentDate'>".$item['currentDate']."</span>"?></p>
<p class ="importance"><?= $item['importance'] ?></p>
<div class="border"></div>
<span><?= $item['text'] ?></span>
</div>
<div class="bottom-content">
<span>Do: <?= date('d.m.Y', strtotime($item['date'])) ?></span>
<form method="post">
<button name="doneButton" value='<?= $item['id'] ?>' type="submit">✅</button>
</form>
<form method="post" >
<button value="<?= $item['id'] ?>" name="deleteButton" type="submit">🗑️</button>
</form>
<?php
if(isset($_POST['deleteButton'])){
db_remove_note($_POST['deleteButton']);
header("Location: list-notes.php");
}
?>
</div>
</li>
<?php
endforeach;
?>
</ul>

add an incrementing number to id with php

I am making a portfolio site with kirby cms.
my site is a single page website. I want to add an incrementing number after id by using foreach in the part shown in the code below..!
Is there easy way to do..?
regards,
<?php foreach ($pages->listed() as $p): ?>
<div class="slideshow">
<div class="slideshow-box">
<div class="slideshow-holder">
<?php foreach ($p->images() as $workimage): ?>
<div class="slide">
<?= $workimage ?><br><br>
</div>
<?php endforeach ?>
</div>
<div class="tooltip">
<p id="myText📍<here>"><?= $p->worktitle() ?></p>
</div>
<div class="toolbox" id="myDIV📍<here>">
<?= $p->description() ?>
</div>
</div>
<div class="actions">
<span>1/3</span>
</div>
←
→
</div>
<?php endforeach ?>
<script>
var button📍<here> = document.getElementById('myText📍<here>');
var div📍<here> = document.getElementById('myDIV📍<here>');
div📍<here>.style.display = 'none';
button📍<here>.onclick = function () {
if (div📍<here>.style.display !== 'none') {
div📍<here>.style.display = 'none';
} else {
div📍<here>.style.display = 'block';
}
};
</script>
I know I am late to the party, but here is my version.
You can actually just set a $counter Variable before the foreach() and just count it up right before the end of the foreach.
Furthermore I would you data-attributes and query them instead of an id.
Here's my Code. I tested it and it worked fine. If there are any questions let me now.
<?php $counter = 1;
foreach ($pages->listed() as $p): ?>
<div class="slideshow">
<div class="slideshow-box">
<div class="slideshow-holder">
<?php foreach ($p->images() as $workimage): ?>
<div class="slide">
<?= $workimage ?><br><br>
</div>
<?php endforeach ?>
</div>
<div class="tooltip">
<p data-tooltip="<?= $counter ?>"><?= $p ?></p>
</div>
<div class="toolbox" data-toolbox="<?= $counter ?>" style="display: none;">
<?= $p ?>
</div>
</div>
</div>
<?php $counter++; endforeach ?>
<script>
const TOOLTIPS = document.querySelectorAll('[data-tooltip]');
TOOLTIPS.forEach(TOOLTIP => {
TOOLTIP.addEventListener('click', () => {
const TOOLBOX = document.querySelector(`[data-toolbox="${TOOLTIP.dataset.tooltip}"]`);
if (TOOLBOX) {
if (TOOLBOX.style.display !== 'none') {
TOOLBOX.style.display = 'none';
} else {
TOOLBOX.style.display = 'block';
}
}
});
});
</script>

how to set the value of array into a input field with php

I have an array
$texts = [ "maybank2u.com",
"Open BillPayment",
"Status: Successful",
"Reference number 2950211545",
"Transaction date: 01 Feb 2016 13:09:17",
"Amount: RM100.00",
"From Account 564155051577 WCAa" ]
HTML
<div class="row">
<div class="col-12">
<ul>
<?php foreach ($texts as $row =>$value): ?>
<li><h3> <?php echo ucfirst($value) ?></h3></li>
<?php endforeach ?>
</ul>
<label>Status:</label><input type="text" name="status" value="" id="status"><br>
</div>
</div>
I tried this solution but it's not working in my case. I want to set the value of $value into the input field
Try This --
<div class="row">
<div class="col-12">
<ul>
<?php foreach ($texts as $row =>$value): ?>
<li><h3> <?php echo ucfirst($value) ?></h3></li>
<?php endforeach ?>
</ul>
<label>Status:</label>
<input type="text" name="status" value="<?php echo ucfirst($texts[2]) ?>" id="status"><br>
</div>
<div class="row">
<div class="col-12">
<ul>
<?php foreach ($texts as $row =>$value): ?>
<li><h3> <?php echo ucfirst($value) ?></h3></li>
<?php endforeach ?>
</ul>
<label>Status:</label><input type="text" name="status" value="<?php echo $texts[2] ?>" id="status"><br>
</div>
</div>
You should use index for your array:
<?php
$texts = [
//...
"status": "Successful",
"reference_number": "2950211545",
"transaction_date": "01 Feb 2016 13:09:17",
"amount": "RM100.00",
"from_account": "564155051577 WCAa"
]
?>
Then:
<input type="text" name="status" value="<?php echo $texts['status'] ?>" id="status">

in_array always returns false?

I want to make checked box checked once its checked for that I am pushing filter ids into the array and whichever filter id coming that should be checked In order to this I am getting all filter in $_GET['filter'] but while Am comparing this with $filter array means in_array always returns false please help me out someone
Below is HTML and PHP code,
<?php echo $header;
if(isset($_GET['filter']))
{
$selected_filter[] = explode(",",$_GET['filter']);
print_r($selected_filter);
// $flag = 1;
}
?>
<div class="wrapper">
<?php echo $column_left; ?>
<div class="filterDiv">
<h3>Filters by :</h3>
<!-- <div class="flList">
<p>price</p>
<div class="flDrop price_Module"><div class="flDropDiv">price div</div></div>
</div> -->
<!-- <div class="list-group-item flList filter_options">
<div class="flDrop price_Module"><div class="flDropDiv">price div</div></div>
<span id="amtmin"></span> - <span id="amtmax"></span>
<input type="hidden" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">
<div id="slider-range"></div>
</div> -->
<div class="flList">
<p>Categories</p>
<div class="flDrop">
<div class="flDropDiv category_fl">
<?php foreach ($categories as $category) {
$category_name = explode("(",$category['name'])
?>
<div class="flItems"> <?php echo $category_name[0]; ?> </div>
<?php }?>
</div>
</div>
</div>
<?php //echo "<pre>"; print_r($filter_groups); die; ?>
<?php foreach ($filter_groups as $filter_group) { ?>
<div class="flList">
<p><?php echo $filter_group['name']; ?></p>
<div class="flDrop">
<div class="flDropDiv">
<?php
// $filter_string = "";
foreach ($filter_group['filter'] as $filter) {
// $filter_string = implode(',',$filter['']);
// print_r($filter_string);
if(isset($selected_filter) && in_array($filter['filter_id'],$selected_filter))
{ ?>
<input name="filter[]" type="checkbox" value="<?php echo $filter['filter_id']; ?>" checked> <?php echo $filter['name'] ?>
<?php } else {?>
<input name="filter[]" type="checkbox" value="<?php echo $filter['filter_id']; ?>"> <?php echo $filter['name'] ?>
<?php }?>
<?php }?>
</div>
</div>
</div>
<?php }?>
<div class="flList">
<?php
$amount_min = $min_product_price;$amount_max = $max_product_price;
if(isset($_GET['amtmin']) && $_GET['amtmin']!=""){
$amount_min = $_GET['amtmin'];
}
if(isset($_GET['amtmax']) && $_GET['amtmax']!=""){
$amount_max = $_GET['amtmax'];
}
?>
<p>Price</p>
<div class="flDrop price_Module">
<div class="flDropDiv">
<!-- <a class="list-group-item fltrHdng">Price</a> -->
<div class="price_slide">
<!-- <label for="amount">Price range</label> -->
<input type="hidden" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">
<div id="slider-range"></div>
<div class="cf mb10"></div>
<span class="pull-left" id="amtmin"></span> <span class="pull-right" id="amtmax"></span>
</div>
</div>
</div>
</div>
<div class="cf"></div>
<?php
if(isset($min_p) && ($max_p)){
?>
<div class="filterSelectPrice filterSelect afr">
<div id="fSprice" class="fSbtn">Rs.<?php echo $min_p; ?> - Rs.<?php echo $max_p; ?> <span class="clear fSp"></span></div>
</div>
<?php } ?>
<div class="filterSelect" id="auto_filter_values"></div>
</div>
</div>
Below is my jQuery code:
<script type="text/javascript">
$('input[name^=\'filter\']').on('change', function() {
filter = [];
$('input[name^=\'filter\']:checked').each(function(element) {
filter.push(this.value);
$(filter).prop('checked',true);
});
// console.log(filter);return false;
window.history.pushState("","",'<?php echo $action; ?>&filter=' + filter.join(','));
//return false;
$.ajax({
url: '<?php echo $action; ?>&filter=' + filter,
type: 'post',
cache: false,
contentType: false,
processData: false,
beforeSend: function() {
$('#content').block({message:'<img src="<?php echo HTTP_SERVER; ?>image/ajax-loader.gif">'});
//$("#content").fadeOut("slow");
},
complete: function() {
// $(this).remove();
//$("#content").fadeIn("slow");
$('#content').unblock();
},
success: function(data) {
$("body").empty().append(data);
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
});
</script>
You are defining a multidimensional array when doing []:
$selected_filter[] = explode(",",$_GET['filter']);
in_array() is looking in the top level which is an array so it won't work. Just do:
$selected_filter = explode(",",$_GET['filter']);

Phpbb show custom fields

I'm trying to show custom profile fields like this in phpbb(3.0.14):
<?php if (!defined('IN_PHPBB')) exit; $this->_tpl_include('overall_header.html'); ?>
<h2><?php echo (isset($this->_rootref['PAGE_TITLE'])) ? $this->_rootref['PAGE_TITLE'] : ''; ?></h2>
<form method="post" action="<?php echo (isset($this->_rootref['S_PROFILE_ACTION'])) ? $this->_rootref['S_PROFILE_ACTION'] : ''; ?>" id="viewprofile">
<div class="panel bg1<?php if ($this->_rootref['S_ONLINE']) { ?> online<?php } ?>">
<div class="inner"><span class="corners-top"><span></span></span>
<dl class="left-box details" style="width: 80%;">
<dd>
<?php $_custom_fields_count = (isset($this->_tpldata['custom_fields'])) ? sizeof($this->_tpldata['custom_fields']) : 0;
if ($_custom_fields_count)
{
for ($_custom_fields_i = 0; $_custom_fields_i < $_custom_fields_count; ++$_custom_fields_i)
{
$_custom_fields_val = &$this->_tpldata['custom_fields'][$_custom_fields_i];
}?>
<dt> <?php echo $_custom_fields_val['PROFILE_FIELD_NAME']; ?>:</dt>
<dd><?php echo $_custom_fields_val['PROFILE_FIELD_VALUE']; ?>
<?php } ?>
</dd>
</dl>
<span class="corners-bottom"><span></span></span></div>
</div>
</form>
<?php $this->_tpl_include('jumpbox.html'); $this->_tpl_include('overall_footer.html'); ?>
But I see nothing while I've got profile fields? What could be wrong here? Thankyou!

Categories