I am building an extention to my site that allows users to favourite posts, I am however having a a problem,
I am running this code in my view,
<?php if(isset($favourites)) : ?>
<?php foreach ($favourites as $fav) : ?>
Fave
<?php endforeach;?>
<?php else : ?>
Fave
<?php endif; ?>
However my problem is that if there are two posts marked as a favourite it will loop over both entries and show the favourite link twice on one post, how can I make it so it loops through the posts and adds a favourite link if that post is indeed a favourite?
If I understand your question:
if (isset($favourites)) {
foreach ($favourites as $fav) {
// i am a favorite
}
} else {
// i am not a favorite
}
If you have the list of links already, which it looks like you do with $j:
foreach ($j as $jj) {
if (isset($favorites[$j])) {
// i am a favourite
} else {
// i am not a favourite
}
}
done.
Related
I have an array with products as objects in it. Each object contains a key = category_title
I have a foreach loop that generates products in Bootstrap rows with configurable columns, in my case $col=3.
What I need is to have a new row if a new category_title comes up.
I created an array to push all the category_titles in it and create a variable if it is a new category. But now I am stuck.
The foreach looks like this:
<?php foreach($productspercat as $product): ?>
<!-- Make sure product is enabled and visible #front end -->
<?php // if($product->enabled && $product->visibility):?>
<?php
$catnew = false;
$categorytitle = $product->category_title;
if(!in_array($categorytitle, $totalcategories, true)):?>
<?php array_push($totalcategories,$categorytitle );
$catnew=true;
?>
<?php endif;?>
<div class="j2store-products-row <?php echo 'row-'.$row; ?> row">
<?php endif;?>
<div class="col-sm-<?php echo round((12 / $col));?>">
<h2><?php echo $categorytitle; ;
var_dump($catnew);
?></h2>
test
</div>
<?php $counter++;
$firstrun = true; ?>
<?php if (($rowcount == $col) or ($counter == $total)) : ?>
</div>
<?php endif; ?>
<?php // endif; ?>
<?php endforeach;?>
So basically if a new category comes up, it should close the row and start a fresh row with a new column count.
appreciate any help!
If you don't want to page refresh then you can do that by using jQuery or Angular JS.
By jQuery you can add data by jquery prepend() function if you wanna add data in starting or append() function to add ending section.
<?php
// Arranged products by category title
$arrangedProducts = array()
foreach($productspercat as $product) {
if (!array_key_exists($product->category_title, $arrangedProducts)) {
$arrangedProducts[$product->category_title] = array();
}
$arrangedProducts[$product->category_title][] = $product;
}
// each category is a key and its value is an array with all its products
foreach ($arrangedProducts as $categoryTitle => $products) {
// you create a new row
// ....
foreach ($products as $product) {
// you display product information
}
// you end the row
}
?>
Code from: oc-content/themes/realestate/item.php
I have this piece of PHP code which is part of a 'framework' called OSClass. Basically, I am trying to 'hide' the user (the publisher of the advert) name to non-logged in users. In other words, only if a user is logged in, only then they can see if the publisher users advert.
I found out that I need to add this piece of code osc_is_web_user_logged_in(), which has worked perfectly for another section, however, due to the else statement, the publishers name is still displaying...
How can I amend the else statement? I could remove the else statement, but I'm worried it will 'break' something and I am unsure what osc_item_user_id() does... Do I add another if statement within the else statement (complete PHP newbie here).
<div class="ico-author ico"></div>
<?php if( osc_item_user_id() != null && osc_is_web_user_logged_in() ){ ?>
<?php echo osc_item_contact_name(); ?>
<?php } else { ?>
<?php echo osc_item_contact_name(); ?>
<?php } ?>
Thanks!
In my opinion you should simple write:
<div class="ico-author ico"></div>
<?php if( osc_item_user_id() != null && osc_is_web_user_logged_in() ){ ?>
<?php echo osc_item_contact_name(); ?>
<?php } ?>
As you don't want to display publisher name when user is not logged, you don't need have else section.
You can add an else if statement.
if (osc_item_user_id() != null && osc_is_web_user_logged_in())
{
<?php echo osc_item_contact_name(); ?>
}
else if (user non logged)
{
// your stuff
}
else
{
<?php echo osc_item_contact_name(); ?>
}
I feel like I know this, but am getting frustrated that I can't remember exactly how to do this.
In PHP, I need to repeat items of a record set in a unordered list. I can repeat items fine, but I don't want anything to show if the record set is empty. Right now, if there is no record, the code is still displaying a blank list item, when I just want nothing to appear.
I have tried this:
<?php do { ?>
<li>Content Goes Here</li>
<?php } while (!feof($recordsetName) && $row_recordsetName = mysql_fetch_assoc($recordsetName)); ?>
And I have tried doing it this way by putting the repeating element withing an if/else statement like this:
<?php if (!feof($recordsetName)) {
echo ""; }
else do { ?>
<li>Content Goes Here</li>
<?php } while ($row_recordsetName = mysql_fetch_assoc($recordsetName));
; } ?>
But that isn't working either. Any information would be helpful
while($row = mysql_fetch_assoc($recordsetName)) {
if($row['fieldname'] != '') {
echo '<li>Content Goes Here</li>';
}
}
or you could check if the query was successful...
$result = mysql_query($sql);
if($result) {
echo '<li>Content Goes Here</li>';
}
or if it returned any results...
$results = mysql_fetch_assoc($recordsetName);
if(mysql_num_rows($results) > 0) {
while($row = mysql_fetch_assoc($results)) {
echo '<li>Content Goes Here</li>';
}
}
Also, you should really be using mysqli or PDO, as mysql is depreciated.
Edit: added loop in example 3.
I'm displaying all the posts in the category that have a valid date as follows -
<?php $blog = $pages->find('posts');
foreach($blog->children() as $blogpost): ?>
<?php if ($blogpost->title() <= $latest && $blogpost->category == $thisCat): ?>
//HTML for displaying post goes here
<?php endif ?>
<?php end foreach ?>
That works fine when those posts validate my condition, and displays nothing if it doesn't. What I want to do is display an error message (like 'there are no posts here') when there are no posts that pass the condition. I can't just do a simple else condition in that if query because it's inside the foreach loop. I can't take the if query out of the foreach loop because it relies on a variable that is defined as part of it ($blogpost).
Kind of stuck in this catch 22... Any suggestions?
How about...
<?php
$blog = $pages->find('posts');
$found_something = false;
foreach($blog->children() as $blogpost) {
if ($blogpost->title() <= $latest && $blogpost->category == $thisCat) {
$found_something = true;
//HTML for displaying post goes here
}
}
if(!$found_something) {
// display error message
}
?>
By the way, is there a specific reason why you're using the alternative PHP syntax?
I'd create a counter variable which increments each time when you display a post.
<?php $blog = $pages->find('posts');
$displayedPosts = 0;
foreach($blog->children() as $blogpost): ?>
<?php if ($blogpost->title() <= $latest && $blogpost->category == $thisCat):
$displayedPosts++; ?>
//HTML for displaying post goes here
<?php endif ?>
<?php end foreach ?>
<?php
if ($displayedPosts == 0) {
echo 'ERROR!';
}
?>
Using a boolean variable (as tmh did) is probably better in this case unless you want to count the posts.
Just count your posts , if it's 0 display just the message , else execute foreach loop :
<?php $blog = $pages->find('posts');
if(count($blog->children())==0){ echo 'No post Here'; }
else{
foreach($blog->children() as $blogpost): ?>
<?php if ($blogpost->title() <= $latest && $blogpost->category == $thisCat): ?>
//HTML for displaying post goes here
<?php endif ?>
<?php end foreach ?>
<?php } ?>
With some if/else PHP statement, I can show or hide some divs depending on if the profile page being checked out by a user belongs to that user or not.
For instance,
$id = $_SESSION['id'];
if ($uid == $id)
{
?>
<div id="block_user"></div>
<?php }
else { ?>
<div id="block_user" style="display:none"></div>
<?php
} ?>
The issue is even if the profile I'm checking out doesn't belong to me, if I inspect the element with Google webmaster tool, and remove
style="display:none"
the div becomes visible again.
How could I prevent that?
just remove the else part
<?php
if ($uid == $id) {
?>
<div id="block_user"></div>
<?php } ?>
If you want to hide stuff for real you have to dynamically create it within the PHP code... Such as :
<php
if ($uid == $id){
echo '<div id="block_user"></div>';
}
?>
which I guess you already are doing but simply dont print it out if you dont want to show it.
instead of hiding the div, remove the div element which you don't want to show.
$id = $_SESSION['id'];
if ($uid == $id)
{
?>
<div id="block_user"></div>
<?php } ?>