can't seem to get the variable to change - php

I am trying to display blog posts from a MySQL data base into an unordered list. I want each row to display the title an image and short description of 3 entries. Here is an example:
<ul class="submissions">
<li class="first">
<a href="url"> <img alt="image Alt" class="blog_image" height="198" src="image url" title="blog title" width="276" />
</a> <div class="submissions_content">
<h3>blog title</h3>
<p> blog discription .</p>
</div>
</li> <li class="second">
<a href="url">
<img alt="image Alt" class="blog_image" height="198" src="image url" title="blog title" width="276" />
</a> <div class="submissions_content">
<h3>blog title</h3>
<p> blog discription .</p>
</div>
</li> <li class="last">
<a href="url">
<img alt="image Alt" class="blog_image" height="198" src="image url" title="blog title" width="276" />
</a> <div class="submissions_content">
<h3>blog title</h3>
<p> blog discription .</p>
</div>
</li> <li class="first">
<a href="url">
<img alt="image Alt" class="blog_image" height="198" src="image url" title="blog title" width="276" />
</a> <div class="submissions_content">
<h3>blog title</h3>
<p> blog discription .</p>
</div>
</li> <li class="second">
<a href="url">
<img alt="image Alt" class="blog_image" height="198" src="image url" title="blog title" width="276" />
</a> <div class="submissions_content">
<h3>blog title</h3>
<p> blog discription .</p>
</div>
</li> <li class="last">
<a href="url">
<img alt="image Alt" class="blog_image" height="198" src="image url" title="blog title" width="276" />
</a> <div class="submissions_content">
<h3>blog title</h3>
<p> blog discription .</p>
</div>
</li></ul>
You see how the li class changes for each entry? I tried this code but the li class ends up being the same for every entry:
<ul class="submissions">
<?php
mysql_connect ('localhost', 'root', '072868') ;
mysql_select_db ('test');
$sql = "SELECT * FROM tbp_blog ORDER BY timestamp DESC LIMIT 5";
$result = mysql_query($sql) or print ("Can't select entries from table tbp_blog.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$date = date("l F d Y g:i:s A", $row['timestamp']);
$link = $row['link'];
$title = stripslashes($row['title']);
$description = stripslashes($row['description']);
$entry = stripslashes($row['entry']);
$image_link = $row['image_link'];
$image_alt = $row['image_alt'];
$id = $row['id'];
$li_class = 'first';
?>
<li class="<?php echo $li_class ; ?>">
<img alt="<?php echo $image_alt; ?>" class="blog_image" height="198" src="<?php echo $image_link; ?>" title="<?php echo $title; ?>" width="276" />
<div class="submissions_content"><h3><?php echo $title; ?></h3>
<p><?php echo $description; ?></p>
</div>
</li>
<?php if ($li_class == 'first') {
$li_class = 'second';
} elseif ($li_class == 'second') {
$li_class = 'last';
} elseif ($li_class == 'fist') {
$li_class == 'first';
}
?>
<?php
}
?>
</ul>
what am I doing wrong?

Move $li_class = 'first'; outside of your while condition. You're resetting it on every iteration.

$li_class = 'first';
put this line OUTSIDE of your while loop:
while($row = mysql_fetch_array($result)) {

Related

How to count foreach loop by range

I'm trying to make the carousel slide image slide by 4 images per slide. I want each slide to display four images, and then the next slide will display another four images. I hope I have explained well.
<?php
$i = 0;
foreach ($photos as $photo) {
if ($i == 0) {
?>
<div class="carousel-item active">
<ul class="thumbnails">
<li class="span3">
<div class="thumbnail">
<i class="tag"></i>
<a href="#">
<img src="<?php echo $photo->image_path(); ?>" width="125" height="125" class="img-thumbnail" alt="buy more"/>
</a>
</div>
</li>
</ul>
</div>
<?php
$i++;
} else {
if ($i != 0) {
?>
<div class="carousel-item">
<ul class="thumbnails">
<li class="span3">
<div class="thumbnail">
<i class="tag"></i>
<a href="#">
<img src="<?php echo $photo->image_path(); ?>" width="125" height="125" class="img-thumbnail" alt="buy more"/>
</a>
</div>
</li>
</ul>
</div>
<?php
}
$i++;
}
}
?>
I believe that will do the trick:
<?php
$numImagesPerSlide = 4;
foreach($photos as $k => $photo){
?>
<div class="carousel-item <?php echo ($k % $numImagesPerSlide == 0) ? 'active': '';?>">
<ul class="thumbnails">
<li class="span3">
<div class="thumbnail">
<i class="tag"></i>
<img src="<?php echo $photo->image_path(); ?>" width="125" height="125" class="img-thumbnail" alt="buy more"/>
</div>
</li>
</ul>
</div>
<?php
}
?>
The point is to use $k, whenever it is divisible by 4 (4, 8, 12, 16, ...), with the function $k % $numImagesPerSlide == 0 then you print the code you need.
I guess you will need an extra code, like
if ($k % $numImagesPerSlide == 0) {
//print the header of the carrousel
}

DOM scripting getElementsByID -- links

I'm learning php and DOM. And i have issue. So, I am trying to improve and understand the following. I have some links with ID div and I am attaching event on them:
<div id='articleList1' >
<div class="hotOne">
<a class="" href="http://link1/index.html" >
<span class="itemTitle">
1 title
</span>
<img src="1.jpg" class=""
alt="1"
title="1"
border="0"
/>
</a>
</div>
<div class="hotThree">
<ul>
<li class="item item1 ">
<a href="http://link2/index.html" title="2" >
<span class="itemTitle">
2 title
</span> <img src="2.jpg" class=""
alt="2"
title="2"
border="0"
/> <p> 2 tekst </span> </p> </a>
</li>
<li class="item item2 ">
<a href="http://link3.html" title="3" >
<span class="itemTitle">
3 title
</span> <img src="3.jpg" class=""
alt="3"
title="3"
border="0"
/> <p> 3 tekst </span> </p> </a>
</li>
</div>
<?php $div1 = $dom->getElementById('articleList1');
$href = $div1->getElementsByTagName('a');
foreach ($href as $hrefs) {
$link = $hrefs->getAttribute('href');
}
echo '<a href ="'.$link.'">'.$link.'<br>'); ?>
However, this script does not work. And I don't know why it's not work?.
Thx for all answers.
Your HTML markup is pretty poor - there are mistakes creeping in which will not help working with the DOM.
<div id='articleList1' >
<div class="hotOne">
<a class="" href="http://link1/index.html" >
<span class="itemTitle">1 title</span>
<img src="1.jpg" class="" alt="1" title="1" border="0" />
</a>
</div>
<div class="hotThree">
<ul>
<li class="item item1 ">
<a href="http://link2/index.html" title="2" >
<span class="itemTitle">2 title</span>
<img src="2.jpg" class="" alt="2" title="2" border="0" />
<p>2 tekst</p><!-- <~~~ error here previously -->
</a>
</li>
<li class="item item2 ">
<a href="http://link3.html" title="3" >
<span class="itemTitle">3 title</span>
<img src="3.jpg" class="" alt="3" title="3" border="0" />
<p>3 tekst</p><!-- <~~~ error here previously -->
</a>
</li>
</ul><!-- missing -->
</div>
</div><!-- missing -->
<?php
$col=$dom->getElementsByTagName('a');
foreach( $col as $node ){
if( $node->nodeType===XML_ELEMENT_NODE && $node->hasAttribute('href') ){
echo $node->getAttribute('href');
}
}
?>
in iteration over $hrefs - refer to dingle href instead of list:
foreach ($href as $hrefs) {
$link = $href->getAttribute('href'); //not $hrefs->
}

how to set html layout in loop

I want to set a html layout view in a loop for multiple categories. My html code is here...there were many of categories came from (wp-admin) woocommerce. Given code is layout of how to show my categories in home.php so i want to add more categories in home.php through loop.Thanks
My html code is So far...
<div class="selected-products-wrapper clearfix">
<div class="category-list">
<span class="category-main" style="background-color: #5D973E">
<div class="category-main_logo">
<img src="" alt="" class="v-middle"></div>
<span class="category-main-title">
<a href="<?php
echo get_term_link($cat->slug, 'product_cat');
?>">
<?php echo $cat->name; ?></a></span>
<div class="category-main-yakataheader"><img src=""></div>
</span>
<ul class="sub-category-list">
<li><?php echo $sub_category ->name; ?></li>
</ul>
View all
</div>
<div class="images-list">
<a href="" class="large-img">
<img src="" alt="Confectionery"
class="v-middle lazy-img" height="351" width="290">
</a>
<span class="small-img-block">
<a href="">
<img src="" alt="Fruits & Veggies"
class="v-middle lazy-img" height="175" width="171">
</a>
<a href="">
<img src="" alt="Noodles"
class="v-middle lazy-img" height="175" width="171">
</a>
</span>
<span class="center_img-block">
<a href="">
<img src="" alt="Oats"
class="v-middle lazy-img" height="175" width="344">
</a>
<a href="">
<img src="" alt="Ofada"
class="v-middle lazy-img" height="175" width="344">
</a>
</span>
<span class="small-img-block last-img-block">
<a href="">
<img src="" alt="Cooking Oils"
class="v-middle lazy-img" height="175" width="171">
</a>
<a href="">
<img src="" alt="Seasoning"
class="v-middle lazy-img" height="175" width="171">
</a>
</span>
</div>
<div class="alt-images-block">
Fruits & Veggies
Noodles
Cooking Oils
Seasoning
</div>
</div>
you need to create a custom php function with the data you want repeat.
e.g.
<?php
function repeat_category(){ ?>
//your html code you want to repeat goes here.
<?php } ?>
Then you can just use
<?php
repeat_category();
?>
Wherever you want your html code to appear.

Yii2 Display Recently Added Members

I have a "Latest Staff Members" panel where it should display my recently added users. All I have is a static display of these users:
<div class="box box-danger <?= !User::isBizAdmin() ? 'hidden' : '' ?>">
<div class="box-header with-border">
<h3 class="box-title">Latest Staff Members</h3>
<div class="box-tools pull-right">
<span class="label label-danger">8 New Members</span>
<button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
<button class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
</div>
</div><!-- /.box-header -->
<div class="box-body no-padding">
<ul class="users-list clearfix">
<li>
<img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/frank_man-128.png" alt="User Image"/>
<a class="users-list-name" href="#">Alexander Pierce</a>
<span class="users-list-date">Today</span>
</li>
<li>
<img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/Ben_man-128.png" alt="User Image"/>
<a class="users-list-name" href="#">Norman</a>
<span class="users-list-date">Yesterday</span>
</li>
<li>
<img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/iri_girl_face-128.png" alt="User Image"/>
<a class="users-list-name" href="#">Jane</a>
<span class="users-list-date">12 Jan</span>
</li>
<li>
<img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/Jay_man-128.png" alt="User Image"/>
<a class="users-list-name" href="#">John</a>
<span class="users-list-date">12 Jan</span>
</li>
<li>
<img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/Fred_man-128.png" alt="User Image"/>
<a class="users-list-name" href="#">Alexander</a>
<span class="users-list-date">13 Jan</span>
</li>
<li>
<img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/michela_face_young-128.png" alt="User Image"/>
<a class="users-list-name" href="#">Sarah</a>
<span class="users-list-date">14 Jan</span>
</li>
<li>
<img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/carla_girl-128.png" alt="User Image"/>
<a class="users-list-name" href="#">Nora</a>
<span class="users-list-date">15 Jan</span>
</li>
<li>
<img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/hena_woman_face-128.png" alt="User Image"/>
<a class="users-list-name" href="#">Nadia</a>
<span class="users-list-date">15 Jan</span>
</li>
</ul><!-- /.users-list -->
</div><!-- /.box-body -->
<div class="box-footer text-center">
View All Users
</div><!-- /.box-footer -->
</div>
Something like that. Now, in the user table in my database, I added a date_added column and I manually put dates in row.
Do I loop the data? Or use a widget? I don't know how else I should implement this.
EDIT
My controller:
public function actionIndex()
{
if(Yii::$app->user->isGuest){
return $this->render('../site/index');
}
else{
$latestStaffMembers = User::find()->orderBy(['date_added' => SORT_DESC])->limit(8);
return $this->render('dashboard', [
'latestStaffMembers' => $latestStaffMembers,
]);
}
}
In my view:
<?php foreach ($latestStaffMembers as $user) { ?>
<li>
<img src="<?php echo $user->avatar; ?>" alt="User Image"/>
<?= $user->username ?>
<span ><?= $user->date_added ?></span>
</li>
<?php } ?>
Then I get this error:
Trying to get property of non-object
I tried var-dumping $user and it says null.
I'm not sure if this is what you want. I hope it helps somehow.
In your controller action:
$latestStaffMembers = User::find()->orderBy(['date_added' => SORT_DESC])->limit(8)->all();
return $this->render('some-view-file', [
'latestStaffMembers' => $latestStaffMembers,
]);
In your view:
<?php foreach ($latestStaffMembers as $user) { ?>
<li>
<img src="<?= $user->imagePath ?>" alt="User Image"/>
<a class="users-list-name" href="#"><?= $user->name ?></a>
<span class="users-list-date"><?= Yii::$app->formatter->asDate($user->dateAdded, 'd m'); ?></span>
</li>
<?php } ?>
Btw: I saw !User::isBizAdmin() in your code. Is it yours? I would not just hide the list if it is important that only admins may see that. Just don't print it if user is not admin.

wordpress footer.php - Parse error: syntax error, unexpected $end

Im getting a,
Parse error: syntax error, unexpected $end in /clientdata/zeus-dynamic-1/c/a/careassess.com.au/www/wp-content/themes/wptheme_careassess/includes/footer.php on line 108
I was trying to remove a section from the footer of my clients site and I managed to break their entire footer. Specifically I was trying to remove the Paypal section under 'éngage'
from line 12-18.
Can some tell me where the bug is? I've tried to read up about it buti don't know what I'm looking for.
<?php if( !$fm_is_home ){ ?>
<div id="footer_subscribe">
<?php echo(apply_filters('the_content', '[contact-form 3 "defaultSubscribe"]')); ?>
</div>
<?php } ?>
<div id="footer_container">
<?php if( !$fm_is_home ){ ?>
<ul id="footerNav">
<li<?php checkSelected($ENGAGE_ID); ?>>
<span class="title">engage</span>
<ul>
<?php echo print_page_navigation($ENGAGE_ID, $PAYPAL_ID); ?>
<li><?php $paypalPage = get_page($PAYPAL_ID); ?>
<?php echo($paypalPage->post_title); ?> <img class="paypal" src="<?php bloginfo('template_directory'); ?>/images/btn_paypal.gif" width="44" height="16" alt="paypal" title="paypal" />
</li>
</ul>
</li>
<li<?php checkSelected($REFER_ID); ?>>
<span class="title">refer</span>
<ul>
<?php echo print_page_navigation($REFER_ID); ?>
</ul>
</li>
<li<?php checkSelected($DELIVER_ID); ?>>
<span class="title">deliver</span>
<ul>
<?php echo print_page_navigation($DELIVER_ID); ?>
</ul>
</li>
<li<?php checkSelected($SUPPORT_ID); ?>>
<span class="title">support</span>
<ul>
<?php echo print_page_navigation($SUPPORT_ID); ?>
</ul>
</li>
<li<?php checkSelected($SHARE_ID); ?>>
<span class="title">share</span>
<ul>
<li>
Our latest news
</li>
<?php echo print_page_navigation($SHARE_ID); ?>
<li>
<span class="follow">Follow us:
<img src="<?php bloginfo('template_directory'); ?>/images/btn_twitter.gif" height="32" width="16" alt="Twitter" title="Twitter"/>
<img src="<?php bloginfo('template_directory'); ?>/images/btn_facebook.gif" height="32" width="16" alt="Facebook" title="Facebook"/>
</span>
</li>
</ul>
</li>
</ul>
<?php } ?>
<div id="footer">
<div class="logo">
<img src="<?php bloginfo('template_directory'); ?>/images/footer_logo_careassess.gif" width="149" height="39" alt="Care Assess" title="Care Assess" />
<span class="copyright">© Copyright 2011 <strong>Care Assess</strong>.</span>
</div>
<ul class="nav">
<li>About us</li>
<li<?php if($current_post_ID == $TERMS_ID){ echo(' class="selected"'); } ?>>Terms</li>
<li<?php if($current_post_ID == $POLICIES_ID){ echo(' class="selected"'); } ?>>Policies</li>
<li<?php if($current_post_ID == $AGREEMENTS_ID){ echo(' class="selected"'); } ?>>Agreements</li>
<li<?php if($current_post_ID == $DISCLAIMER_ID){ echo(' class="selected"'); } ?>>Disclaimer</li>
<li<?php if($current_post_ID == $FEEDBACK_ID){ echo(' class="selected"'); } ?>>Feedback</li>
<li<?php if($current_post_ID == $CREDITS_ID){ echo(' class="selected"'); } ?>>Credits</li>
<li<?php if($current_post_ID == $SITEMAP_ID){ echo(' class="selected"'); } ?>>Sitemap</li>
<?php if( $fm_is_home ){ ?>
<li><span id="seoShow">More</span><span id="seoHide">Less</span></li>
<?php } ?>
<li<?php if($current_post_ID == $CONTACT_US_ID){ echo(' class="selected"'); } ?>>Contact Us</li>
</ul>
<?php if (!has_parent_page($SHARE_ID) && $SHARE_ID != $current_post_ID) { ?>
<div class="controls">
<form action="http://www.google.com/translate" id="language" target="_blank">
<fieldset>
<input name="u" value="<?php bloginfo('url'); ?>" type="hidden" />
<input name="hl" value="en" type="hidden" />
<input name="ie" value="UTF8" type="hidden" />
</fieldset>
<fieldset>
<img class="google_icon" src="<?php bloginfo('template_directory'); ?>/images/google_icon.gif" width="19" height="20" alt="google" title="google" />
<select name="langpair" onchange="document.forms['language'].submit()">
<option value="en|en">English</option>
<option value="en|fr">French</option>
<option value="en|de">German</option>
<option name="langpair" value="en|it">Italian</option>
<option name="langpair" value="en|pt">Portuguese</option>
<option name="langpair" value="en|es">Spanish</option>
<option name="langpair" value="en|ja">Japanese</option>
<option name="langpair" value="en|ko">Korean</option>
<option name="langpair" value="en|zh-CN">Chinese Simplified</option>
</select>
</fieldset>
</form>
<ul>
<li class="addthisLi">
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style">
<a class="rss btn" href="http://www.careassess.com.au/feed/"><img src="<?php bloginfo('template_directory'); ?>/images/btn_addthis_rss.png" width="25" height="48" alt="RSS" title="RSS" /></a>
<a class="addthis_button_facebook btn"><img src="<?php bloginfo('template_directory'); ?>/images/btn_addthis_facebook.png" width="25" height="48" alt="Facebook" title="Facebook" /></a>
<a class="addthis_button_twitter btn"><img src="<?php bloginfo('template_directory'); ?>/images/btn_addthis_twitter.png" width="25" height="48" alt="Twitter" title="Twitter" /></a>
<a class="addthis_button_google btn"><img src="<?php bloginfo('template_directory'); ?>/images/btn_addthis_google.png" width="25" height="48" alt="Google" title="Google" /></a>
<a class="addthis_button_email btn"><img src="<?php bloginfo('template_directory'); ?>/images/btn_addthis_email.png" width="25" height="48" alt="Email" title="Email" /></a>
<!--<a class="addthis_button_compact btn"><img src="<?php
Any help would be greatly appreciated!
I've certainly learnt my lesson about editing theme files when your a noob :(
You have to close if conditions,i doesn't saw whether it's has been closed and line 108 not fully shown in given link please can you give proper code.
<?php if (!has_parent_page($SHARE_ID) && $SHARE_ID != $current_post_ID) { ?>
didn't see that being closed, unless I am mistaken.
Add:
<?php } ?>
To fix the issue!
Add following line in last
</div></li></ul>
<?php } ?>
</body>
</html>

Categories