I got an array of news items parameters that I loop trough and get the results out
is there any way I loop the array for specific items before I do the foreach ,
I want to loop only trough post with specific category which is fetched before the foreach
$get_cats = $k2tools_params->get('cloud_category');
returns an array of allowed categories
than I have
<?php foreach($this->items as $key=> $item): >
<?php if (in_array($item->category->id,$get_cats)){ ?>
// do something
<?php} ?>
<?php endforeach ?>
<?php echo $this->pagination->getPagesLinks(); ?>
this works fine but the issue I have is the pagination wich counts 18 items from $this->items
and I have 3 pages of results , but in foreach with my category restriction I have only 2 items , and this is causing additional 2 empty pages.
so I must restrict the categories befre the foreach , or loop trough $this->items first
thnx for any help , I hope you understand .
dont mind the php tags separating html from php this is joomla templating
what I kinda want to do is foreach where category id is in array ,
In order to that properly with pagination, you will have to find some way to modify the original SQL query.
foreach(array_keys($items, $get_cats)) {
// do_something
}
or try to use array_search
Related
<?php
foreach($products as $product){
//some code here , echoing products and everything works there
foreach($types as $type){
echo $type['type'];
// only does the echo on the first occurence of this "types" loop
}
}
?>
Hi everyone,
I'm making a type of form in order to allow people, for each product of the database, to specify the type of the product.
$products and $types are both distinct arrays. Products contains all my products and $types contains all my types.
Actually I have only 2 products and 2 types.
So, my question is: Why the echo doesn't show anything for the second occurence of the foreach($products as $product), and show the good results at the first occurence?
This is exactly the same array, called 2 times, but working only at the first occurence of the loop. Very Strange for me.
Hope you'll understand despite my poor english.
Bye
Let's assume there are 4 products and 7 types. With the following code you are executing the outer loop 4 times and the inner loop 7 times. What is printed is the content of $type['type'] for all 7 items. And this 4 times.
<?php
foreach($products as $product){
foreach($types as $type){
echo $type['type'];
}
}
?>
What you need is a $products array that contains the types for the product, so that you can use the following code
<?php
foreach($products as $product){
foreach($product->type as $type){
echo $type['type'];
}
}
?>
This happens because on the second loop, $type is still a reference to the last array item, so it's overwritten each time.
You can see it like that:
$types = array ('type-one','type-two','type-three');
foreach ($types as $type) {
echo $type;
}
foreach ($types as $type) {
echo $type;
}
As you can see, the second time you call the array. It will not echo anything.
There is no "bug" in your code. It just happens like that
To solve it. You can use this code before the second loop. It will reset the array reference.
reset($types);
i have an array result. i want to print 2 rows(product data) in first page.
next 2 rows in second page and so on. if anybody knows this,please help me to solve it
my array
$data['product_list']
foreach($data['product_list'] as $dat)
{
echo $dat->prd_id;
echo $dat->prd_name;
}
You are doing a foreach loop on an associative array and then trying to access the the contents as objects by using ->. I can only given assumption of what you might be doing. If your array is already populated with a name and id like you have described in your foreach loop this is how you would access the contents in the loop:
foreach($data['product_list'] as $dat)
{
echo $dat['prd_id'];
echo $dat['prd_name'];
}
That is how you would print out the contents providing you had the data stored in your array like so:
$data['product_list'][0] = array('prd_id'=>'id0','prd_name'=>'name0');
$data['product_list'][1] = array('prd_id'=>'id1','prd_name'=>'name1');
$data['product_list'][2] = array('prd_id'=>'id2','prd_name'=>'name2');
Better you try with array_slice();
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>
I'm just transitioning over to PDO from mysql_
I have been pointed in the direction of some helpful tutorials, but there is one issue I haven't seen raised and I wanted to check I am doing it in the 'correct way'
I want to reuse the same values that I am dropping into an array in two different areas (both different PHP code blocks) within one page. Both are styled differently depending on media queries (hidden/not hidden etc.)
At the moment I am running a query like this:
<?php
$data = $pdo->query("SELECT * FROM sitelinks WHERE `show` = 'yes' ORDER BY `Order` ASC")->fetchAll();
foreach ($data as $links)
{
echo "\n<li class=\"linkitem\">{$links['Text']}</li>";
}
?>
So my understanding is that I should then be able to load and loop through the $links variable somewhere else on the page. I am doing that like this:
<?php
foreach ($data as $links)
{
echo "\n<li class=\"desktoplinkitem\">{$links['Text']}</li>";
}
?>
Is that correct? It's working, but seems a little crazy to use ($data as $links) again. Following on, really stupid question but why does the $data variable then have to be stored as $links. Could it not just be run as foreach ($links) to begin with?
Assuming you do not write some code later that changes/destroys the $data variable between these 2 usages there is no problem doing this. It in fact reduces the runtime to reuse data if you can rather than getting it again.
What you need to remember is that ->fetchAll() returns all the rows from your resultset into an array. Its your array from then on, which you can do whatever you like with once the ->fetchAll() is completed
Second question:
the foreach
foreach ($data as $links)
processes over an array $data and returns one occurance (row in your case) at a time. So you have to give it another variable name. That name can be anything.
If you use sensible names for things they normally look like this
foreach ($rows as $row)
foreach ($sitelinks as $sitelink)
Using the plural for the original array and the singular for the single occurance returned by each iteration over the array.
So I would amend your code like so:
<?php
$sitelinks = $pdo->query("SELECT *
FROM sitelinks
WHERE `show` = 'yes'
ORDER BY `Order` ASC")
->fetchAll();
foreach ($sitelinks as $sitelink) {
echo "\n<li class=\"linkitem\">{$links['Text']}</li>";
}
?>
And the second use of the array
<?php
foreach ($sitelinks as $sitelink) {
echo '\n<li class=\"desktoplinkitem\">{$sitelink['Text']}</li>";
}
?>
I have a listings table, and each listing has column called 'top'. Basically, if top is not null, I want that listing to show up first, then the others.
Right now, my code is like this:
foreach ($results as $result):
echo $result->name;
endforeach;
This will show me everything, but I want the rows with 'top' not null to show up first. What's the best way to go about this?
Can't you apply an ORDER BY to the query that feeds $results?
I agree with Aaron... the best thing to do is to order them so that they are first in the query.
If for some reason you couldn't do that you could do a few other things:
Sort the object or array in PHP... may have to use a custom function .. if that's the case see: Sort Object in PHP
Grab the rows that only have the NULL first, print them, then grab the ones that aren't null.
You could loop through the array twice. First only printing the NULLs, then again to print the others.
Try
foreach ($result as $row){
Then specify each element you want in order.
echo $row['top']
echo $row['your next data row']...
and have them space exactly how you want.
And if you need it formated, try using <pre> first.
You can also add a if statment.
$data = $row['top']
if($top == 1) { then echo this } else {echo the values that are null }
Based on your comment below #Aaron Bertrand's answer, you would need an order clause in your sql like:
ORDER BY ISNULL(`top_ad`), the_rest_of_your_order_clause
I'm trying to check if a certain category is allready selected by looping through an array of categories also I want to add another element to the array whci is just a bit to indicate is the category selcated
my categories array looks like this
0=>array(category_id=>12,category_name=>"blogger")
1=>array(category_id=>13,category_name=>"dancer")
etc...
now the code i'm trying goes like that:
foreach ($userCategories as $key=>$category) {
if($category['category_id'] == $mediaDetails['currentCategory']) {
$category['current'] = 1;
} else {
$category['current'] = 0;
}
}
when executing
die(var_dump($userCategories));
I expect to get an array similar to
0=>array(category_id=>12,category_name=>"blogger",current=>0)
1=>array(category_id=>13,category_name=>"dancer",current=>1)
but instead I get the same array I had before the foreach loop
any ideas?
Thanks
It looks like $category is not getting passed by reference.
Try $userCategories[$key]['current']=1 instead, and see how that works.