Sort Foreach Loop after ID - php

Is it possible to sort the result of a foreach loop after the ID? With the highest ID first and so on... Since it's articles I want the highest ID (newest) to be first :)
My code:
include_once('includes/connection.php');
include_once('includes/article.php');
$article = new Article;
$articles = $article->fetch_all();
?>
<html>
<head>
<title>CMS</title>
<link rel="stylesheet" href="assets/style.css" />
</head>
<body>
<div class="container">
CMS
<ol>
<?php foreach ($articles as $article) { ?>
<li>
<a href="article.php?id=<?php echo $article['article_id']; ?>">
<?php echo $article['article_title']; ?>
</a>
- <small>
Posted <?php echo date('l jS', $article['article_timestamp']); ?>
</small>
</li>
<?php } ?>
</ol>
<br />
<small>Admin Login</small>
</div>
</body>
</html>

You're looking for sorting the array $articles according to article_id field - which can be done using the function usort.
Example:
<?php
function cmp($a, $b)
{
if ($a['article_id'] == $b['article_id']) {
return 0;
}
return ($a['article_id'] < $b['article_id']) ? -1 : 1;
}
usort($articles, "cmp");
foreach ($articles as $article) {
echo "$article: ". $article['article_id']." \n";
}

Related

Article title showing multiple times in foreach loop

Article title is showing multiple times in foreach loop
Here is the articles.php
<?php
include('functions.php');
$article = new Article;
$articles = $article->fetch_all();
?>
<html>
<head>
<title>Sample Blog</title>
<link rel = "stylesheet" type = "text/css" href = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<div class="col-sm-6">
<h1 class="text-center">Articles</h1> </div>
<ol>
<?php
foreach($articles as $article){?>
<li>
<a href="article.php?id=<?php echo $articles["article_id"];?>">
<?php echo $articles["article_title"];?>
</a>
</li>
<?php
}
?>
</ol>
</div>
</body>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</html>
And here is the functions.php code
<?php
require ('config.php');
if (!$connection) {
echo "Failed to connect to Server!".mysqli_connect_error();
}
class Article{
public function fetch_all(){
$connection = mysqli_connect(host,username,password,database) or die("Unable to connect to the host");
$select = "SELECT * FROM articles";
$result = mysqli_query($connection,$select) or die(mysqli_error($connection));
return (mysqli_fetch_assoc($result));
}
}
?>
Here is the screenshot of the output
The problem is within your loop. You are calling $articles["article_id"] and $articles["article_title"].
You want $article["article_id"] and $article["article_title"].
foreach($articles as $article){?>
<li>
<a href="article.php?id=<?php echo $article["article_id"];?>">
<?php echo $article["article_title"];?>
</a>
</li>
<?php
}

Get a Primary Key from <a href> for it's content to be visible on another page with Code Igniter

I am completely new to Code Igniter, and heres my issue:
I am making a Cinema Website, and im trying to get the all of the movies resume
in one page, for example, when i pick a movie on the index, it should open me
a new link that gives me the movie details that i got on my database.
This is the php from the index
Filmes
<div id="films-tab">
<?php
$lista = $this->db->get('filme');
foreach($lista->result() as $row) {
?>
<li><?php echo $row->Nome; ?></li>
<?php
}
?>
</div>
</ul>
</li>
This is the php for the "briefing page"
<link href="assets/recursos/outro.css" rel="stylesheet">
<div class="container-fluid">
<?php
$query = $this->db->get_where('filme', ['id' => $id]);
$lista = $this->db->get('filme');
foreach($lista->result() as $row) {
$row = $query->first_row();
?>
<img style="margin-right:20px; border:5px solid white; width:320px; height:468px;"src="<?php echo 'assets/recursos/images/'.$row->Capa; ?>">
<?php
}
?>
</div>
I know the code to show only 1 key in the briefing.php is wrong, but i have no clue how to get it to work, can someone help me?
pass id in url as parameter and get data from database of that id.
pass id in index page's url
<div id="films-tab">
<?php
$lista = $this->db->get('filme');
foreach($lista->result() as $row) {
?>
<li><?php echo $row->Nome; ?></li>
<?php
}
?>
</div>
</ul>
</li>
and fetch id from url in briefing page
<link href="assets/recursos/outro.css" rel="stylesheet">
<div class="container-fluid">
<?php
$id = $this->uri->segment(2);
$query = $this->db->get_where('filme', ['id' => $id]);
$lista = $this->db->get('filme');
foreach($lista->result() as $row) {
$row = $query->first_row();
?>
<img style="margin-right:20px; border:5px solid white; width:320px; height:468px;"src="<?php echo 'assets/recursos/images/'.$row->Capa; ?>">
<?php
}
?>
</div>

Opencart Display subcategories

on each products page I currently have displayed the products subcategory, but only one level deep. So if in categories I have Brother > MFC > B4564 > TN450.... and I am on the TN450's product page, down below it will only show BN4564 . I need it also show The mfc and the brother part. I am not sure how to do this.. any ideas? here is my current code.
.TPL file
<!-- Display product categories -->
<?php foreach ($catprod as $catp)
{
?><?php echo $catp['name']; ?> <?php
}`
controller .php file
$data['catprod'] = array();
$product_category = $this->model_catalog_product->getCategories($this->request->get['product_id']);
foreach ($product_category as $prodcat)
{
$category_info = $this->model_catalog_category->getCategory($prodcat['category_id']);
if ($category_info)
{
$data['catprod'][] = array('name' => $category_info['description'],'href' => $this->url->link('product/category', 'path=' . $category_info['category_id']));
}
}`
which version of opencart?
this should work for version 1:
open catalog/view/theme/default/template/module/category.tpl
remove its contents and insert:
<div class="box" id="categories-menu">
<div class="box-heading"><?php echo $heading_title; ?></div>
<div class="box-content">
<ul class="box-category" id="box-category">
<?php foreach ($categories as $category) { ?>
<li>
<?php if ($category['category_id'] == $category_id) { ?>
<a href="<?php echo $category['href']; ?>" class="active list-group-item" ><?php echo $category['name']; ?></a>
<?php } else { ?>
<?php echo $category['name']; ?>
<?php } ?>
<?php if ($category['children']) { ?>
<ul class="submenu">
<?php foreach ($category['children'] as $child) { ?>
<li>
<?php if ($child['category_id'] == $child_id) { ?>
- <?php echo $child['name']; ?>
<?php } else { ?>
- <?php echo $child['name']; ?>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>

Getting value with array

I am struggling to get the value of an element in a particular array. I would like to get the value of the "logo" in this case to return "Logo Google 2013 Official.svg" from the code below.
Any help is greatly appreciated.
<html>
<head>
</head>
<body>
<html>
<body>
<h2>Search</h2>
<form method="post">
Search: <input type="text" name="q" value="google" />
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST['q'])) {
$search = $_POST['q'];
$url_2 =
"http://en.wikipedia.org/w/api.php?
action=query&prop=revisions&rvprop=content&format=json&titles=$search&rvsection=0&continue=";
$res_2 = file_get_contents($url_2);
$data_2 = json_decode($res_2);
?>
<h2>Search results for '<?php echo $search; ?>'</h2>
<ol>
<?php foreach ($data_2->query->pages as $r):
?>
<li>
<?php foreach($r->revisions[0] as $a);
echo $a; ?>
</li>
<?php endforeach; ?>
</ol>
<?php
}
?>
</body>
</html>
The resulting $url_2 is http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=google&rvsection=0&continue=
Use a regular expression to capture what you want:
<ol>
<?php foreach ($data_2->query->pages as $r): ?>
<?php foreach($r->revisions[0] as $a): ?>
<li>
<?php
preg_match_all('/ logo += +([^|]+)/', $a, $result, PREG_PATTERN_ORDER);
echo trim($result[0][0]); // prints 'Logo Google 2013 Official.svg'
?>
</li>
<?php endforeach; ?>
<?php endforeach; ?>
</ol>

processing list of items in sections

I want 3 item per div (the sample has a total of 7 items) like this:
<div>
<item/>
<item/>
<item/>
</div>
<div>
<item/>
<item/>
</div>
But I can't do
while($r = mysql_fetch_array($q){
?><item/><?
}
if(++$i%3==1){
//blabla:)
}
Because it incorrectly prints out
<div>
<item/>
<item/>
<item/>
</div>
<div>
<item/>
...
How do I correctly print out the items in blocks of 3?
You were most of the way there.
$result = mysql_query("SELECT ....");
$recordCounter = 0;
$record = mysql_fetch_assoc($result);
while ($record) {
if ($recordCounter % 3 == 0) {
?>
<div class="item-block">
<?php
}
?>
<div class="item"></div>
<?php
$record = mysql_fetch_assoc($result);
$recordCounter++;
if ($record === false || $recordCounter % 3 == 0) {
?>
</div>
<?php
}
}
mysql_free_result($result);
somewhat shortened :))
<div class="items">
<? $q = mysql_query("SELECT * FROM table ");
$i=1;
while($r = mysql_fetch_array($q))
{
?><item /><?
if($i%4==0){?></div><div class="item"><? }?>
<? $i++;
}?>
</div>
Here is another option, maybe there are too much php tags =)
<?php
$items = array(1,2,3,4,5,6,7,8);
$count = 1;
?>
<html>
<body>
<?php foreach ($items as $item) : ?>
<?php if ($count == 1) : ?>
<div>
<?php endif; ?>
<p><?php echo $item; ?></p>
<?php if ($count == 3) : ?>
</div>
<?php $count = 0; ?>
<?php endif; ?>
<?php $count++; ?>
<?php endforeach; ?>
</body>
</html>

Categories