Trouble adding certain number of rows with Codeigniter - php

I have a script running through Codeigniter that pulls up a list of images pertaining to a certain product from the database. The max number of images per product is 10 but that is not always what is in the database. Thus, I am trying to pull from the database and complete the list of images even if it is less than 10. For instance,
I have
Image 1
Image 2
Image 3
Image 4
from the database. On the view, I am trying to still display the final six images (with blank data of course) so on the page itself it still shows the containers.
This is the code I have compiled so far from other advice and suggestions.
EDIT: My question essentially is how can I complete all 10 spots for images even if what I pull from the database is less than that as referenced in the example above?
From product model...
public function getImages($id) {
//run query
$this->db->from('images_main');
$this->db->where('images_main.product_id',$id);
$this->db->limit(10);
$this->db->order_by('img_order', 'asc');
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return $query->result_array();
}else{
$this->session->set_flashdata( 'message', 'The query was not successful.' );
}
}
From view...
<ul id="sortable">
<?php
$count = count($img);
$max = 10;
$total = $max - $count;
for($t=1; $t<=$total; $t++):
$img[] = $t;
endfor;
print_r($img);
for ($i=1; $i<=10; $i++):
?>
<li class="img_list">
<div class="photo_content">
<div class="thumbnail" id="<?php echo $i; ?>"><img src="/crop.php?w=100&h=100&f=http://www.apriltwentyfive.com/product_images/regular_images/<?php echo $img[$i]['img_name'];?>"/></div>
<div class="uploads"><input type="text" name="photo_order" size="1" value="<?php echo $img[$i]['img_order']; ?>" readonly="readonly"/></div>
<div class="browse"></div>
</div>
</li>
<?php endfor; ?>
</ul>

As I understand your code, the variable $count contains the number of entries from the database.
So instead of having a for loop to 10, set it to $count (or use a foreach on $img).
Your $total variable is not the total but how many blank images there are to fill in. Have another loop for that.
Put both loops inside the <li class="img_list">...<li> tag.
<?php
$count = count($img);
$total = 10;
$difference = $total - $count;
?>
<ul id="sortable">
<?php
// Images from the database
for ($i = 0; $i <= $count - 1; $i++):
?>
<li class="img_list">
<div class="photo_content">
<div class="thumbnail" id="<?php echo $i; ?>"><img src="/crop.php?w=100&h=100&f=http://www.apriltwentyfive.com/product_images/regular_images/<?php echo $img[$i]['img_name'];?>"/></div>
<div class="uploads"><input type="text" name="photo_order" size="1" value="<?php echo $img[$i]['img_order']; ?>" readonly="readonly"/></div>
<div class="browse"></div>
</div>
</li>
<?php endfor; ?>
<?php
// Blank images
for ($i = 0; $i <= $difference - 1; $i++):
?>
<li class="img_list">
<div class="photo_content">
<div class="thumbnail">
<img src=".../path/to/blank_photo.png" alt="Title">
</div>
</li>
<!-- rest of HTML code -->
<?php endfor; ?>
</ul>
Hope this helps.
Notes:
IDs always start by 0, you should therefore start your loop at 0 and
not 1
<img> tags must have an alt property

Related

Check If bootstrap last column is even or odd

I have bootstrap column in while loop, what I want is if bootstrap last column is odd then I want column to be 12 (col-12), I found the way to check number even or odd but want to check last number so if last number (last column) is odd I want column to be 12 else remain col-6
I have tried:
<div class"<?php echo ($i == (2 || 4 || 6) )?'col-md-6':'col-md-12'; ?>">
display content there in while loop
</div>
<div class"<?php echo ($i % 2 == 0)?'col-md-6':'col-md-12'; ?>">
display content there in while loop
</div>
but could get the job done...
You make a mistake in writing HTML. But version with modulo must work:
<div class="row">
<?php for ($i = 0; $i < 100; $i++) { ?>
<div class="<?php echo ($i%2 === 0) ? 'col-md-6' : 'col-md-12'; ?>">
display content there in while loop
</div>
<?php } ?>
</div>
solution By m_hutley
Where $i = counter;
$total = acf repeater count;
<div class="<?php echo ($i == $total && $total % 2 !== 0 )?'col-md-12':'col-md-6'; ?>" id="<?php echo $i; ?>">
<h1><?php echo "contain"; ?></h1>
</div>

Instagram API script - Count images and display max 10

Below you can see the script I use to access the "api" and display max 10 images. That works so far, but if a user has less than 10 images, it errors. How can I display MAX 10 images and if the user has less than 10, then display only the amount of images the user has?
Script:
<?php
// Get data from Instagram User
$url = "https://www.instagram.com/wsj/media/";
$instagramdata = #file_get_contents($url);
$instagramdata = json_decode($instagramdata, true);
?><div class="instagramfeed"><?php
for($i = 0; $i < 10; $i++){
$instagramimages = $instagramdata['items'][$i]['images']['standard_resolution']['url'];
$instagrampage = $instagramdata['items'][$i]['code'];
$instagramlikes = $instagramdata['items'][$i]['likes']['count'];
?><a href="http://instagram.com/p/<?=$instagrampage?>" target="_blank"><?php
?><div class="instagramimages" style="background:url(<?=$instagramimages?>)no-repeat center;background-size:cover;"><?php
?>
<div class="instagramimagesoverlay">
<span class="instagramimagesoverlaytext"><i class="fa fa-heart fa-1x" aria-hidden="true"></i> <?php echo $instagramlikes;?></span>
</div>
<?php
?></a><?php
?></div><?php
}
?>
You need to set the Count for the number of items in the array, there are a number of ways of doing that but here is one
You also seem to have some unnecessary <?php .. ?> pairs with nothing in them, so I removed those too.
<?php
// Get data from Instagram User
$url = "https://www.instagram.com/wsj/media/";
$instagramdata = #file_get_contents($url);
$instagramdata = json_decode($instagramdata, true);
?>
<div class="instagramfeed">
<?php
$cnt = count($instagramdata['items']) > 10 ? 10 : count($instagramdata['items']);
for($i = 0; $i < $cnt; $i++){
$instagramimages = $instagramdata['items'][$i]['images']['standard_resolution']['url'];
$instagrampage = $instagramdata['items'][$i]['code'];
$instagramlikes = $instagramdata['items'][$i]['likes']['count'];
?>
<a href="http://instagram.com/p/<?=$instagrampage?>" target="_blank">
<div class="instagramimages" style="background:url(<?=$instagramimages?>)no-repeat center;background-size:cover;">
<div class="instagramimagesoverlay">
<span class="instagramimagesoverlaytext"><i class="fa fa-heart fa-1x" aria-hidden="true"></i> <?php echo $instagramlikes;?></span>
</div>
</a>
</div> <!-- this probably belongs outside the foreach loop-->
<?php
}
?>

Use PHP to increment number for image ID

So, I have a problem in that I have 67 (and counting) images all named IMAGE01.jpg, IMAGE02.jpg all the way to IMAGE67.jpg.
In my HTML page, I have this code to display the image.
<article class="thumb">
<img src="images/thumbs/image01.jpg" alt="" />
</article>
So, I have this for 67 images. Is there a way of incrementing the image number using PHP? So I can just copy and paste that piece of code for the number of images I have?
That's the first part that would be GREAT to have a solution too. But, would there be a way to not have to repeat that code for every image? Would it be possible to use PHP to define the number of times I want this to be repeated?
So have the snippet of code once and have something to repeat it a defined number of times so as I add to the collection, all I have to do is change the repeat times?
Many thanks in advance for the help. Means allot.
<?php
for($i=1; $i<=67; $i++) {
?>
<article class="thumb">
<a href="images/fulls/image<?php
if($i<9)
echo "0".$i;
else
echo $i;
?>.jpg" class="image"><img src="images/thumbs/image<?php
if($i<9)
echo "0".$i;
else
echo $i;
?>.jpg" alt="" /></a>
</article>
<?php
}
?>
To make what you want you need to use a for loop from 1 to 67 (because your image name are from 1 to 67). And for each iteration you have to echo the html string.
Like this for example :
for($i = 1; $i<=67; $i++){
$name = $i<10?"0".$i:$i;
echo "<article class='thumb'>
<a href='images/fulls/image".$name.".jpg' class='image'>
<img src='images/thumbs/image".$name.".jpg' alt=''/>
</a>
</article>";
}
You need to construct the name if the name need to have 0 before (01, 02 ...). It's what I make here :$name = $i<10?"0".$i:$i;. It's mean that if $i < 10 then we add a "0" before else the name is $i.
You can also use a while loop :
$i = 1;
while($i<=67){
$name = $i<10?"0".$i:$i;
echo "<article class='thumb'>
<a href='images/fulls/image".$name.".jpg' class='image'>
<img src='images/thumbs/image".$name.".jpg' alt=''/>
</a>
</article>";
$i++;
}
It's the same idea.
Hope that helps you.
You can use a PHP variable and inject it into your HTML. The following code will count from 1 - 67.
<?php
$count = 1;
while($count <= 67){
?>
<p>Count: <?php echo $count; ?></p>
<?php
$count +=1;
}
?>
So you could use it as such:
<article class="thumb">
<?php
$count = 1;
while($count <= $numOfImages){
?>
<img src="images/thumbs/image<?php echo $count; ?>.jpg" alt="" />
<?php
$count +=1;
}
?>
</article>
Just set $numOfImages to however many images there are and it should work.
You can try this sample code below if images are one after the other. Using single for loop with printf inside echo...
<body>
<?php
for ($i = 0; $i < 67; $i++) {
?>
<article class="thumb">
<a href="images/full/image<?php printf("%02d\n", $i)?>.jpg" ><img src="images/thumb/image<?php printf("%02d\n", $i)?>.png" alt="" /></a>
</article>
<?php
}
?>
</body>
Here is the sample output I kept only one image for testing. http://i.stack.imgur.com/l1Lo5.png

show only three records and then move to next line

This is a very basic problem but I am very new to PHP.
I need to show results in such scenario that only three records in same line then add <br /> and then on next line, same thing should happend. I am unable to make its logic and in a great trouble :(
Right now, I am just using the simple way i.e.
while($data = mysql_fetch_array($res_set)) {
?>
<div><?php echo $data['name']?><img src="images/<?php echo $data['image']?>" /></div>
<?php
}
this is my code
$cnt = 0;
while ($prd = mysql_fetch_array($res)) {
?>
<div class="imageRow">
<?php
$cat_id = $prd['cat_id'];
$sql = "select * from tbl_category where id = $cat_id";
$cat_res = mysql_query($sql);
$cat_data = mysql_fetch_array($cat_res);
$cat_name = $cat_data['name'];
?>
<div class="set">
<div class="single first">
<img src="<?php echo $ru ?>admin/product_images/<?php echo $cat_name ?>/large/<?php echo $prd['thumb_img'] ?>" style="height: 100px; width: 100px;" /><br />
Choose
</div>
</div>
</div>
<?php
$cnt = $cnt++;
if($cnt%3 == 0) {echo "<br />";}
}
//echo $cnt;
?>
any help will be appreciated.
Thanks
<div class="imageRow">
<?php
$i = 0;
while($data = mysql_fetch_array($res_set)) { ?>
<div>
<?php echo $data['name']; ?><img src="images/<?php echo $data['image']; ?>" />
</div>
<?php
$i++;
if($i % 3 == 0) {
echo '</div><div class="imageRow"><br />';
}
}
?>
</div>
Explanation:
Set the variable $i to a number, which will then be used to keep track of how many items you've written.
Then, within the while loop, increment $i ($i++), which is the same as $i = $i + 1; By doing this, you always know which item you're on - whatever the value of $i is. Some people choose to set it to 1 initially then increment at the very end - other like to set it to 0, and set it near the beginning - either is completely fine - whatever you need it to do / whichever way you like better.
Lastly, check if $i is evenly divisible by 3 (kind of like remainder - it's called "mod" and is represented by the percent symbol). If it is, then echo a line break.
Try this
<?php
echo "<div>";
while($data = mysql_fetch_attay($res_set)) {
?>
<div style="width:30%; float:left"><?php echo $data['name]?><img src="images/<?php echo $data['image']?>" /></div>
<?php
}
echo "</div>";
?>
Output
<div>
<div>1</div> <div>2</div> <div>3</div>
<div>1</div> <div>2</div> <div>3</div>
<div>1</div> <div>2</div> <div>3</div>
</div>
That's some super ugly code you have there. This will do what you need, and is cleaner.
<?php
$i=1;
while($data = mysql_fetch_attay($res_set)) {
$frame = '<div><img src="%s" /></div>';
printf($frame, $data['image']);
if($i % 3 == 0) { echo '<br />'; }
$i++;
}
Also, <img> elements are already block elements to begin with, you really don't need to wrap another <div> around them.

Order data in three columns instead of one

At the moment, with the code from below, I have the data shown like this:
http://img27.imageshack.us/img27/8083/29769986.jpg
but I want it to be shown like this:
http://img259.imageshack.us/img259/3233/24033830.jpg
The code for the data shown, as it is on the first image, is:
<div id="content">
<?php foreach ($categories as $category) { ?>
<div class="manufacturer-list">
<div class="manufacturer-heading"><?php echo $category['name']; ?><a id="<?php echo $category['name']; ?>"></a></div>
<div class="manufacturer-content">
<?php if ($category['manufacturer']) { ?>
<?php for ($i = 0; $i < count($category['manufacturer']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['manufacturer']) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['manufacturer'][$i])) { ?>
<li><?php echo $category['manufacturer'][$i]['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
</div>
</div>
<?php } ?>
</div>
I order to get the "Hewlett-Packard" text under the "HTC" text, I've changed the "/ 4" into "/ 1", but I have no idea how to make the data to be shown into three columns (like on the second picture), instead of one, as it is now (as shown on the first picture).
Thanks in advance.
EDIT: What I actually need, is to count and to do the calculation on this code:
<?php foreach ($categories as $category) { ?>
.
.
.
<?php } ?>
So it needs to count the number of categoris, do the calculations, and present the code between into three columns.
Try this one.
<div id="content">
<div class="content-column">
<?php
$cols = 3; // Change to columns needed.
$catcount = count($categories);
$catpercol = ceil($catcount / $cols);
$c = 0;
foreach ($categories as $category) {
if ( $c == $catpercol ) {
$c = 0;
print "</div><div class='content-column'>";
}
?>
<div class="manufacturer-list">
<div class="manufacturer-heading"><?php echo $category['name']; ?><a id="<?php echo $category['name']; ?>"></a></div>
<div class="manufacturer-content">
<?php if ($category['manufacturer']) { ?>
<?php for ($i = 0; $i < count($category['manufacturer']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['manufacturer']) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['manufacturer'][$i])) { ?>
<li><?php echo $category['manufacturer'][$i]['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
</div>
</div>
<?php $c++; } ?>
</div>
</div>
Add .content-column { float: left; width: 33.33333%; } to your CSS.
Details:
$cols = 3; enables you to set the desired number of columns (note: you might need to change CSS accordingly).
$catcount = count($categories); gives you the total number of categories about to be rendered.
$catpercol = ceil($catcount / $cols); divides that total number evenly into the required number of columns with the last column having eventually less items than the others.
$c = 0; is your counter. It increases at the end of the outer foreach loop.
Within the loop, $cis checked if it matches the $catpercol number and if so, the current parent div is closed and a new one created. You end up with as many parent divs as you need columns. Just add appropiate CSS to make them appear besides each other.
understand the following code that according to your requirement, the code just give the hint to achieve that you want according to your given screen shoot http://img259.imageshack.us/img259/3233/24033830.jpg
echo "<table>";
echo "<tr>";
$i = 1;
do{
// column range
$range = 3;
echo "<td>" . $i;
if( $i % $range == 0 ){
echo "</tr>";
echo "<tr>";
}
echo "</td>";
$i++;
}while( $i <= 10 );
echo "</tr>";
echo "</table>";
Hope this will help you
I think it is possible to create three column layout via html/css and without any change of your PHP code. Just use float:left; width: 33%. You can also use absolute value for width property because of margins and borders.

Categories