there are currently three posts but when onclick alerts only one post.If I echo the posts insite the loop,then all three posts are shown,however if I alert them,then onlyone posts is show.plz help or suggest any alternative approach.
$sql=mysqli_query($db3,"SELECT * from user where id='$id'");
$num_rows=mysqli_num_rows($sql);
while($row=mysqli_fetch_array($sql)){
$posts=$row['posts'];
}
?>
<span onclick=u(<?php echo $posts; ?>)> <?php echo $num_rows ?> </span>
<script type="text/javascript">
function u(posts) {
alert(posts);
}
</script>
<?php
Update
Here is second query
After using the array approach ,If i use fancy box with $num rows to show all posts in the fancybox .I am again getting the only one result on the fancybox.Plz help
$posts[] = $row['posts'];
}
foreach ($posts as $af){
echo "<div id='#modelbox_id'>$af</div>";}
?>
<?php echo $num_rows; ?>
<?php
It is because you are assigning a single post row into the $posts variable, and then overwriting that variable with a new value on each iteration of the while loop.
Try something like this instead:
$posts = array();
while($row=mysqli_fetch_array($sql)){
$posts[] = $row['posts'];
}
Then you will need to print each value of the $posts array.
For example, you could implode the array into a string:
<span onclick=u(<?php echo implode(', ', $posts); ?>)> ...
Instead try something like this
while($row=mysqli_fetch_array($sql)){
$posts .= $row['posts'].",";
}
?>
<span onclick="u('<?php echo $posts; ?>')"> <?php echo $num_rows ?> </span>
<script type="text/javascript">
function u(posts) {
alert(posts);
}
</script>
Related
`hi guys am trying from 4 days am not getting . how to retrieve multiple images from using explode functionality.Now I need to explode that image in view . i getting single images from an array but not retrieveing multiple images in my database have one row contains three images how to return result from model.
and show in the view file
myview file:
`<div class="carousel-inner" >
<?php echo $this->session->flashdata('message')?>
<?php $item_class = 'active';
foreach($newzw as $result){?>
<div class="item <?php echo $item_class; ?> ">
<div class="overlay"></div>
<?php $img=explode(",",base_url().'uploads/images/'.$result->image);
//print_r($img);exit;
?>
<img src="<?php echo $img[0] ?> " alt="Los Angeles" style="height:662px;" >
</div>
<?php $item_class = '';
} ?>
</div>
<!-- Left and right controls -->
</div>
</div>
my model code:
function uploadslide1()
{
$this->db->select('*');
$this->db->order_by("id", "DESC");
$query = $this->db->get("sg_slides");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
controller:
function index()
{
$data=$this->general();
$data['newzw']=$this->public_model->uploadslide1();
$data['body']="static/body";
$this->load->view('welcome',$data);
}
I'm just telling you how to using explode.
The explode() function breaks a string into an array. If you want to break your image value into an array format then you have to do this
explode(separator,string); // Syntax
In your case , plays a role of separator.
$img = explode(",", $result->image); //store image value as an array format in img
Now you have the $img variable as an array now you can perform itteration using foreach
example:
foreach($img as $src){
echo '<img src="'.base_url().'uploads/images/'.trim($src)." />';
}
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
}
?>
I have another error anymore, when I try to display a result in my view, the result is NULL and I can't see the result of my query at models.
Here's my code list :
on the controller (home.php) :
$data['hasil5'] = $this->home_model->popular_list();
on the model (home_model.php) function popular_list() :
function popular_list($limit=2)
{
$this->db->select('news.*');
$this->db->where('id',$this->uri->segment(3));
$this->db->where('publish',1);
$this->db->where('viewed >= ',5);
$this->db->order_by('id','DESC');
$this->db->limit($limit);
$query = $this->db->get('news');
return $query->result();
} //thanks to kumar_v
and on my view (home.php) as a part of "Popular news" :
<h2>Most Popular News :</h2>
<?php
foreach ($hasil5 as $data5):
?>
<div class="welcome clear"><img class="imgl" src="<?php echo base_url(); ?>assets/news/original/<?php echo $data5->image; ?>" alt="" height="119" width="125" />
<div class="fl_right">
<h2><?php echo anchor($data5->kategori.'/detail/'.$data5->id,$data5->title) ?></h2>
<p><?php echo $data5->sinopsis; ?></p>
</div>
</div>
<?php
endforeach;
?>
The results is NULL, can you correct it again? thanks..
try this :
function popular_list($limit = 2) {
$query = $this->db->select('*')
->where('id',$this->uri->segment(3))
->where('publish',1)
->where('viewed >= ',5)
->order_by('id','DESC')
->limit($limit)
->get('news');
print_r($query->result());
return $query->result();
}
You can put the whole thing in $query variable. And also, you can improve your $this->db->where() by putting the params in an array.
You should fix your code to look like so:
<?php
foreach ($data['hasil5'] as $data5):
?>
You were using the wrong variable in your foreach loop.
Checking if the query will return any row is a good idea.
function popular_list($limit=2)
{
$result = null;
$this->db->select('news.*');
$this->db->where('id',$this->uri->segment(3));
$this->db->where('publish',1);
$this->db->where('viewed >= ',5);
$this->db->order_by('id','DESC');
$this->db->limit($limit);
$query = $this->db->get('news');
if($query->num_rows() > 0)
{
$result = $query->result();
}
return $result;
}
I have the code below that is selecting a random set of questions from Wordpress.
<?php
$rows = get_field('step_by_step_test');
$row_count = count($rows);
$rand_rows = array();
$questions = get_field('select_number_of_questions');
for ($i = 0; $i < min($row_count, $questions); $i++) {
$r = rand(0, $row_count - 1);
while (array_search($r, $rand_rows) !== false) {
$r = rand(0, $row_count - 1);
}
$rand_rows[] = $r;
echo $rows[$r]['question'];
}
?>
I want to incorporate a bit of extra code (below), how can I make sure it's selecting the same random question?
<?php if(get_sub_field('answer_options')): ?>
<?php while(has_sub_field('answer_options')): ?>
<?php echo the_sub_field('answer'); ?>
<?php endwhile; ?>
<?php endif; ?>
Why dont you change your approach slightly?
<?php
$rows = get_field('step_by_step_test'); // Get the test
$question_count = get_field('select_number_of_questions'); // Get the number of questions
$rows = shuffle($rows); // Randomize your questions
$rows = array_slice($rows, $question_count); // Now set the array to only contain the number of questions you wanted
foreach ($rows as $row) {
echo $row['question']; // Show the question
if(get_sub_field('answer_options', $row['id'])) {
while(has_sub_field('answer_options', $row['id'])) {
echo the_sub_field('answer');
}
}
}
?>
I made the assumption that you can alter "get_sub_field" to include the ID of the question, so you can then include the ID in your "where" field of "answer_options". This will allow you to link the question.
I think that what you need is to set up the whole thing in a loop. query by custom field
Or you could store the ids of the questions you got above, an then, below, query for the answers for those specific posts.
Here's how I randomized my WordPress slider using the Advanced Custom Fields plugin + Royal Slider with a modified version of TheSwiftExchange's code above
<div id="full-width-slider" class="royalSlider heroSlider rsMinW">
<?php
/*
* Slider Repeater field shuffled
* http://stackoverflow.com/questions/12563116/incorporating-extra-loop-into-random-selection
*/
$rows = get_field('slider');
// For Debugging:
// echo "<pre>";
// var_dump($rows);
// echo "</pre>";
$quotes = get_field('slide_text'); // Get the number of images
shuffle($rows); // Randomize your slides
foreach ($rows as $row) {
$slideImageID = $row['slide_image'];
$slideImage = wp_get_attachment_image_src( $slideImageID, 'full' );
$slideText = $row['slide_text'];
?>
<div class="rsContent">
<div class="royalCaption">
<div class="royalCaptionInner">
<div class="infoBlock">
<?php if(!empty($slideText)) {
echo $slideText;
}; ?>
</div>
</div>
</div>
<img src="<?php echo $slideImage[0]; ?>" class="" />
</div><!-- /.rsContent -->
<?php } // end foreach ?>
</div><!-- /slider-wrap -->
I am relatively new to The Zend framework having only been working with it probably two months at the most. I am now trying to get a list of results from a query I run, send it through the zend paginator class and display 4 results per page, however it does not seem to be recognising when it has got 4 results, I believe there is an error in my code but I can not for the life of me see it, I was hoping someone here will be able to pick up on it and help me and out and probably tell me it is something stupid that I have missed. Thanks here is the code I have written.
This the query in my model
public function getAllNews()
{
$db = Zend_Registry::get('db');
$sql = "SELECT * FROM $this->_name WHERE flag = 1 ORDER BY created_at";
$query = $db->query($sql);
while ($row = $query->fetchAll()) {
$result[] = $row;
}
return $result;
}
This is code in my controller
function preDispatch()
{
$this->_helper->layout->setLayout('latestnews');
Zend_Loader::loadClass('newsArticles');
$allNews = new newsArticles();
$this->view->allNews = $allNews->getAllnews();
//die (var_dump($this->view->allNews));
$data = $this->view->allNews;
// Instantiate the Zend Paginator and give it the Zend_Db_Select instance Argument ($selection)
$paginator = Zend_Paginator::factory($data);
// Set parameters for paginator
$paginator->setCurrentPageNumber($this->_getParam("page")); // Note: For this to work of course, your URL must be something like this: http://localhost:8888/index/index/page/1 <- meaning we are currently on page one, and pass that value into the "setCurrentPageNumber"
$paginator->setItemCountPerPage(1);
$paginator->setPageRange(4);
// Make paginator available in your views
$this->view->paginator = $paginator;
//die(var_dump($data));
}
Below is the view that builds the paginator,
<?php if ($this->pageCount): ?>
<div class="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
< Previous |
<?php else: ?>
<span class="disabled">< Previous</span> |
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<?= $page; ?> |
<?php else: ?>
<?= $page; ?> |
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
Next >
<?php else: ?>
<span class="disabled">Next ></span>
<?php endif; ?>
</div>
<?php endif; ?>
And finally the code the makes up my view
<div id="rightColumn">
<h3>Latest News</h3>
<?php if (count($this->paginator)): ?>
<ul>
<?php foreach ($this->paginator as $item): ?>
<?php
foreach ($item as $k => $v)
{
echo "<li>" . $v['title'] . "</li>";
}
?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?= $this->paginationControl($this->paginator, 'Sliding', '/latestnews/partial_pagination_control.phtml'); ?>
</div>
I will be greatful for any help you can give me.
Thanks
Sico
$paginator should be set to 4 as you want to show only 4 records at a time :
$paginator->setItemCountPerPage(4);
So I think the root of your problem is in your model class with the getAllNews function. The Zend_Db fetchAll function retrieves an associative array of all the records matching your query. You are returning an array of one element that is an array of all the rows retrieved by your select statement.
Try this:
public function getAllNews()
{
$db = Zend_Registry::get('db');
$sql = "SELECT * FROM $this->_name WHERE flag = 1 ORDER BY created_at";
return $db->fetchAll($sql);
}
You sould not pass values to paginator if you want it to limit your query.
There is a code will work for you
$usersTable = $this->getTable();
$registerSelect = $usersTable->select()->where('status = ?', 'OK')->order('lastLogin DESC');
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($registerSelect));
$paginator->setCurrentPageNumber($page);
$paginator->setItemCountPerPage($limit);