I would add each two result one div class
<?php while ($fetch = $db->fetch($query)) { ?>
<?php echo $fetch['title']; ?>
<?php } ?>
Output should be like this
<div class="one">
<div class="two">
<article>Title</article>
<article>Title</article>
</div>
</div>
<div class="one">
<div class="two">
<article>Title</article>
<article>Title</article>
</div>
</div>>
<?php
$i=0;
while ($fetch = $db->fetch($query)) { ?>
<?php if ($i%2==0) { ?>
<div class="one">
<div class="two">
<?php } ?>
<article><?php echo $fetch['title']; ?></article>
<?php if ($i++%2==1) { ?>
</div>
</div>
<?php } ?>
<?php } ?>
//Also is a good idea to verify if the <div> tags are closed
<?php if ($i%2==1) { ?>
</div>
</div>
<?php } ?>
$count = 0;
while ($fetch = $db->fetch($query))
{
if ($count == 0)
echo '<div class="one"><div class="two">';
if ($count < 2)
echo '<article>'.$fetch['title'].'</article>';
if ($count == 2)
{
echo '</div></div>';
$count = 0;
}
$count++;
}
Related
I have a repeater using Advanced Custom fields, which when a count reaches a total (of 3) based on an iterative count, I would like to output a div, then reset the counter.
Here's the code, at the moment it's not outputting as it should and I don't know enough about math to get it to work.
Appreciate your assistance :-)
<?php if(have_rows('flexible_row')) : ?>
<div class="row">
<?php if(empty($count)){ $count=1;} while(have_rows('flexible_row')) : the_row(); ?>
<?php if(get_sub_field('column_width') == "One column") { $count = $count+1; ?>
<div class="col-sm-4">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
Count is <?php echo $count; ?>
<hr/>
</div>
<?php } if(get_sub_field('column_width') == "Two columns") { $count = $count+2; ?>
<div class="col-sm-8">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
Count is <?php echo $counter; ?>
<hr/>
</div>
<?php } else { $count = $count+3; ?>
<div class="col-sm-12">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
Count is <?php echo $counter; ?>
<hr/>
</div>
<?php } if ($count == 3) { ?></div><div class="row"><?php $count = 0; } ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
Think I'd got my logic mixed up a bit — I wasn't using the else clauses correctly. Here's the final working code in case anyone stumbles across this in the future:
<?php if(have_rows('flexible_row')) : ?>
<div class="row">
<?php if(empty($count)){ $count=0;} while(have_rows('flexible_row')) : the_row(); ?>
<?php if(get_sub_field('column_width') == "One Column") { $count = $count+1; ?>
<div class="col-sm-4">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
</div>
<?php } elseif(get_sub_field('column_width') == "Two Columns") { $count = $count+2; ?>
<div class="col-sm-8">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
</div>
<?php } else { $count = $count+3; ?>
<div class="col-sm-12">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
</div>
<?php } if ($count == 3) { ?></div><div class="row"><?php $count = 0; } ?>
<?php endwhile; ?>
</div>
You could of course use switch statements too..
<div class="puffar">
<?php
//Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
//Get children
$children = ($post->post_parent) ? get_page_children($post->post_parent, $all_wp_pages) : get_page_children($post->ID, $all_wp_pages);
$i = 0;
//Build custom items
echo "<div class='row'>";
foreach ($children as $child) {
?>
<div class="col-sm-6">
<div class="puff">
<div class="puff-image-holder">
<?php echo get_the_post_thumbnail($child->ID, 'full'); ?>
</div>
<fieldset class="linedHeadline hlmedium">
<legend><?php echo get_the_title($child->ID); ?></legend>
</fieldset>
<?php echo get_field("puff_introtext", $child->ID); ?>
<?php
$values = get_field('puff_lanktext', $child->ID);
if (get_field("popup_eller_lank", $child->ID) == "popup") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class="linktopage open-popup"
href="<?php echo get_page_link($child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a>
</legend>
</fieldset>
<?php
} elseif (get_field("popup_eller_lank", $child->ID) == "extern") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class="linktopage"
href="<?php echo get_field("puff_lank", $child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a>
</legend>
<?php
$i++;
if ($i % 2 == 0) {
echo "</div><div class='row'>";
}
} else {
}
?>
</div>
</div>
<?php } ?>
</div>
</div>
I want every 2 items that's rendered out to be wrapped in a <div class="row">, however I can't figure it out. Can anyone help me?
So basically the row should wrap every 2 elements that is getting looped. I have been stuck on this forever... Hopefully anyone got better expertise than me hehe.
The div="row" should wrap the col-sm-6 and class="puff".
$i = 0;
foreach ($children as $child) {
$i++;
// your code goes here
if($i % 2 == 0) {
echo "</div><div class='row'>";
// reset the counter to 0
$i = 0 ;
}
}
use proper logic
if ($i % 2 == 0) {
echo "</div><div class='row'>";
}
$i++;
First check if its mod by 2 or not (Gives 0 value after MOD), then close div , open new.
Now increase counter . Because for the first time , i will be 0 , then you increment it and then you use logic. So in short counter shoul be incremented at the end only not in between before you do any operation/logic.
Updated
Use code as it is **: issue was you have i++ and your condition in 3rd else if which never executed. So took it outside All and just before foreach.
<div class="puffar">
<?php
//Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
//Get children
$children = ($post->post_parent) ? get_page_children($post->post_parent, $all_wp_pages) : get_page_children($post->ID, $all_wp_pages);
$i = 0;
//Build custom items
echo "<div class='row'>";
foreach ($children as $child) {
?>
<div class="col-sm-6">
<div class="puff">
<div class="puff-image-holder">
<?php echo get_the_post_thumbnail($child->ID, 'full'); ?>
</div>
<fieldset class="linedHeadline hlmedium">
<legend><?php echo get_the_title($child->ID); ?></legend>
</fieldset>
<?php echo get_field("puff_introtext", $child->ID); ?>
<?php
$values = get_field('puff_lanktext', $child->ID);
if (get_field("popup_eller_lank", $child->ID) == "popup") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class="linktopage open-popup"
href="<?php echo get_page_link($child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a>
</legend>
</fieldset>
<?php
} elseif (get_field("popup_eller_lank", $child->ID) == "extern") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class="linktopage"
href="<?php echo get_field("puff_lank", $child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a>
</legend>
<?php
} else {
}
?>
</div>
</div>
<?php
if ($i % 2 == 0) {
echo "</div><div class='row'>";
}
$i++;
} ?>
</div>
</div>
First set $i=0;
if ($i % 2 == 0) {
echo "</div><div class='row'>";
}
$i++;
I have a while loop and inside the while loop there is an if-else statement. All I want to do is put all the contents of the else statements into one div. It's in Wordpress.
I can't seem to get the logic behind it..
<?php /* The loop */
$i = 1;
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
static $count = 0;
if ($count == 5) { break; }
else {
?>
<div class="content_container">
<?php
if($i == 1) {
?><span style="width:370px;" class="main active"><?php the_post_thumbnail('full');?></span><?php
} else {
?><span class="child nonactive"><?php the_post_thumbnail('full'); ?></span><?php
}
$i++;
$count++;
?>
</div>
<?php
}
?>
<?php endwhile; ?>
<div class="content_container">
<span <?php if($i == 1) { ?> style="width:370px;" class="main active" <?php } else {?> class="child nonactive" ><?php the_post_thumbnail('full');?></span>
<?php }
$i++;
$count++;
?>
</div>
I am trying to apply delete function on selected record in my application. the problem is that the record are displayed in pagination ie 3,4 rows, i am able to delete record from the first page but when i move to the second page and select few records, the delete record buton doesn't work. any adivise will really help.
I am looking for replies
here is my code for listings
public function index($offset=0)
{
if ( $this->session->userdata('u_name') == FALSE )
{
$data['page_title']="Admin Login";
redirect('admin/login',"refresh",$data);
}
$this->load->model('candidate_model');
$limit=4;
$results=$this->candidate_model->get_all_Candidate($limit,$offset);
$data['candidates']=$results['rows'];
$offset = $this->uri->segment(3, 0);
$data['num_results']=$results['num_rows'];
$this->load->library('pagination');
$config=array();
$config['first_url'] = 'candidateList/index';
$config['base_url']=site_url('candidateList/index');
$config['total_rows']=$data['num_results'];
$config['per_page']=$limit;
$config['uri_segment']=3;
$config['first_link'] = '>';
$config['first_tag_open'] = '<div>';
$config['first_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['page_title']="Candidate List";
$this->load->view('manageCandidate',$data);
}
my code for delete function is bellow
if($this->input->post('Delete')=='Delete')
{
for($i=0;$i<count($_POST['checkbox']);$i++)
{
$candidate_id = $_POST['checkbox'][$i];
$this->candidate_model->deleteCandidate($candidate_id);
$this->session->set_flashdata('deleteSelected','Selected Candidate has been deleted.');
}
$data['page_title']="Candidate List";
redirect('candidateList',$data);
}
Delete function is here:
public function candidate()
{
if ( $this->session->userdata('u_name') == FALSE )
{
$data['page_title']="Admin Login";
redirect('admin/login',"refresh",$data);
}
$checkList=$this->input->post('checkbox');
$this->load->model('candidate_model');
if($checkList!=NULL)
{
if($this->input->post('Delete')=='Delete')
{
for($i=0;$i<count($_POST['checkbox']);$i++)
{
$candidate_id = $_POST['checkbox'][$i];
$this->candidate_model->deleteCandidate($candidate_id);
$this->session->set_flashdata('deleteSelected','Selected Candidate has been deleted.');
}
$data['page_title']="Candidate List";
redirect('candidateList',$data);
}
else if($this->input->post('Email')=='Email')
{
for($i=0;$i<count($_POST['checkbox']);$i++)
{
$admin_id = $_POST['checkbox'][$i];
$email_to[$i] = $_POST['checkbox'][$i];
$data['email_to'] = $email_to;
}
$data['page_title']='Send Mail';
$this->load->view('admin/sendmail4',$data);
}
}
else
{
$this->session->set_flashdata('deleteSelect','Please Select at-least one Candidate.');
$data['page_title']="Candidate List";
redirect('candidateList',$data);
}
}
ManageCandidate code:
<?php
$this->load->view('includes/template3');
?>
<?php
$this->load->view('includes/superAdminMenu');
?>
<div class="dashboard">
Add Candidate
<?php if($this->session->flashdata('deleteCandidate')) : ?>
<p class="successMsg"><?php echo $this->session->flashdata('deleteCandidate')?></p>
<?php endif; ?>
<?php if($this->session->flashdata('editCandidate')) : ?>
<p class="successMsg"><?php echo $this->session->flashdata('editCandidate')?></p>
<?php endif; ?>
<?php if($this->session->flashdata('deleteSelected')) : ?>
<p class="successMsg"><?php echo $this->session->flashdata('deleteSelected')?></p>
<?php endif; ?>
<?php if($this->session->flashdata('deleteSelect')) : ?>
<p class="noRows"><?php echo $this->session->flashdata('deleteSelect')?></p>
<?php endif; ?>
<?php if($this->session->flashdata('msgSent')) : ?>
<p class="successMsg"><?php echo $this->session->flashdata('msgSent')?></p>
<?php endif; ?>
<?php
if($num_results==0)
{ ?>
<p class="noRows"><?php echo "You have not added any Candidate.";?></p>
<?php }
else
if($num_results>0)
{ ?>
<form action="candidateList/candidate" method="post" name="sendMail" class="addformClass" id="candidateList1">
<!-- Script by hscripts.com -->
<!-- copyright of HIOX INDIA -->
<!-- Free javascripts # http://www.hscripts.com -->
<script type="text/javascript">
checked=false;
function checkedAll (candidateList1) {
var aa= document.getElementById('candidateList1');
if (checked == false)
{
checked = true
}
else
{
checked = false
}
for (var i =0; i < aa.elements.length; i++)
{
aa.elements[i].checked = checked;
}
}
</script>
<!-- Script by hscripts.com -->
<?php if(isset($candidates)) { ?>
<div class="candidateTable">
<div class="candidateRowHeading">
<div class="candidateHeadingChkBox">
<h4><input type='checkbox' name='checkall' onclick='checkedAll(candidateList1);'>
</h4>
</div><!--END candidateHeadingChkBox-->
<div class="candidateColHeading1">
<h4>Candidate Name</h4>
</div><!--END candidateColHeading1-->
<div class="candidateColHeading3">
<h4>Email</h4>
</div><!--END candidateColHeading3-->
<div class="candidateColHeading4" style="display:none">
<h4>Status</h4>
</div><!--END candidateColHeading4-->
<div class="candidateColHeading6">
<h4>Recruiter Assigned</h4>
</div><!--END candidateColHeading6-->
<div class="candidateColHeading5">
<h4>Action</h4>
</div><!--END candidateColHeading5-->
</div><!--END candidateRowHeading-->
<?php $count=0; ?>
<?php foreach ($candidates as $candidate) { ?>
<div class="candidateRowData">
<div class="candidateColChkBox">
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $candidate->candidate_id; ?>">
</div><!--END candidateColChkBox-->
<div class="candidateColData1">
<?php echo anchor('admin/viewCandidate/'.$candidate->candidate_id, $candidate->first_name." ".$candidate->last_name); ?>
</div><!--END candidateColData1-->
<div class="candidateColData3">
<h4><?php echo $candidate->email; ?></h4>
</div><!--END candidateColData3-->
<div class="candidateColData4" style="display:none">
<h4><?php echo $candidate->lead_status; ?></h4>
</div><!--END candidateColData4-->
<div class="candidateColData6">
<h4>
<?php if($candidate->recruiter_id_fk!=0) { ?>
<?php echo get_recruiterFirstName($candidate->recruiter_id_fk); ?>
<?php echo get_recruiterLastName($candidate->recruiter_id_fk); ?>
<?php echo " (<strong>"; ?>
<?php echo get_recruiterLogin($candidate->recruiter_id_fk); ?>
<?php echo "</strong>) "; ?>
<?php }
else if($candidate->recruiter_id_fk==0) { ?>
<?php echo "Not Assigned"; ?>
<?php } ?>
</h4>
</div><!--END candidateColData6-->
<div class="candidateColData5">
<?php echo anchor('admin/editCandidate/'.$candidate->candidate_id,'Edit'); ?>
<?php echo anchor('admin/deleteCandidate/'.$candidate->candidate_id,'Delete'); ?>
</div><!--END candidateColData5-->
</div><!--END candidateRowData-->
<?php $count++; } ?>
</div><!--END candidateTableCom-->
<?php } ?>
<?php } ?>
<?php if(isset($candidates)) { ?>
<div id="pageNum">
<?php echo $this->pagination->create_links(); ?>
</div>
<?php } ?>
<?php
if($num_results>0) {
echo '<input type="submit" name="Delete" class="emailAllBtn" value="Delete" />';
echo '<input type="submit" name="Email" class="emailAllBtn" value="Email" />';
echo "</form>";
} ?>
</div>
<?php
$this->load->view('includes/footer2');
?>
<?php
function get_recruiterFirstName($id)
{
$CI =& get_instance();
$mod = $CI->load->model('recruiter_model');
$count = $CI->recruiter_model->get_recruiterFirstName($id);
return $count;
}
?>
<?php
function get_recruiterLastName($id)
{
$CI =& get_instance();
$mod = $CI->load->model('recruiter_model');
$count = $CI->recruiter_model->get_recruiterLastName($id);
return $count;
}
?>
<?php
function get_recruiterLogin($id)
{
$CI =& get_instance();
$mod = $CI->load->model('recruiter_model');
$count = $CI->recruiter_model->get_recruiterLogin($id);
return $count;
}
?>
Small mistake exist in your pagination code offset value correct it
public function index($offset=0)
{
if ( $this->session->userdata('u_name') == FALSE )
{
$data['page_title']="Admin Login";
redirect('admin/login',"refresh",$data);
}
$this->load->model('candidate_model');
$limit=4;
//whenever you are calling this pagination offset value you need to fetch it from URL
$offset_url = $offset = $this->uri->segment(3, 0);
//if it's not exist take the default value
$offset = is_numeric($offset_url)?$offset_url:$offset;
$results=$this->candidate_model->get_all_Candidate($limit,$offset);
$data['candidates']=$results['rows'];
..............
}
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>