Let's say I have a model that represents products in a catalog. The model supplies the content provider (CActiveDataProvider) to the view, which in turn uses it to display a grid (CGridView).
What I need is a custom way to display this data: custom next/previous page links, custom data presentation. Something along these lines:
<div class="pagination">
<a class="arrows prev fl" href="#"><span class="icon"></span>back</a>
<a class="arrows next fr" href="#">forward <span class="icon"></span></a>
</div>
<ul class="some class">
<li class="item"><img src="image.jpg" width="100" height="200"/></li>
<li class="item"><img src="image.jpg" width="100" height="200"/></li>
<li class="item"><img src="image.jpg" width="100" height="200"/></li>
<li class="item"><img src="image.jpg" width="100" height="200"/></li>
<li class="item"><img src="image.jpg" width="100" height="200"/></li>
<li class="item"><img src="image.jpg" width="100" height="200"/></li>
</ul>
What is the best way to do this? So far, I am considering the following:
Use CGridView, but somehow customize its output using parameters.
Create a class as a descendant of CGridView or even CBaseListView and put in the formatting logic in it.
Just do it the quick and dirty way: simply iterate over items and echo the HTML.
Or maybe I am missing something here and there's a better way?
Bootstrap is good for displaying tabular data. I use Yii with Bootstrap to generate my own data display, simply because I don't like Yii's CGridView, even though it's convenient.
Your controller function might look something like this:
public function actionList($offset=0){
$model = new CatalogModel;
$data = $model->listCatalogsItems($offset);
$this->render('list',array(
'model'=>$data,
));
}
and your model like this
public function listCatalogItems($offset=0) {
$query = SELECT *
FROM catalog
WHERE <your conditions here> LIMIT 10 OFFSET " . $offset;
$items = Yii::app()->db->createCommand($query)
->queryAll();
return $items;
}
So with your next / previous page buttons, pass the OFFSET as a value in the url, ex http://yoursite.com/site/action/2
Hope that makes sense.
I ended up creating my own class to display this data as a descendant of CWidget class.
All the logic went into the run method.
Related
I have 2 tables:
TABLE users (id, name, image, user_group_id)
TABLE user_groups (id, name, menu)
column menu value contains something like this:
<img src="../images/employees/'.$row_UserDetails['Image'].'" alt="">
Now at img src its creating a problem.
'.$row_UserDetails['Image'].'
This doesn't display the value.
Why is that and how do we fix this?
I want to display results from recordset UserGroup like this:
Now the value coming from php mysql is: <img src="../images/employees/'.$row_UserDetails['Image'].'" alt="">
you see .$row_UserDetails['Image'].
This value must be abc.jpg which comes from Recordest UserDetails:
On this php page if i want to display user name, i use php echo like this: <?php echo $row_UserDetails['Name'];?>
When using echo in php you will need to escape the quotes as they cause problems.
<header id=\"header-navbar\" class=\"content-mini content-mini-full\"> <ul class=\"nav-header pull-right\"> <li> <div class=\"btn-group\"> <button class=\"btn btn-default btn-image dropdown-toggle\" data-toggle=\"dropdown\" type=\"button\"> <img src=\"../images/employees/\"" .$row_UserDetails['Image']. "\" alt=\"\"> <span class=\"caret\"></span> </button> <ul class=\"dropdown-menu dropdown-menu-right\"> <li> <a tabindex=\"-1\" href=\"my-profile.php\"> <i class=\"si si-user pull-right\"></i> My Profile </a> </li> <li> <a tabindex=\"-1\" href=\"../logout.php\"> <i class=\"si si-logout pull-right\"></i>Log out </a> </li> </ul> </div> </li> </ul>
</header>
You say you echo $row_UserDetails['Image'], but I don't see an echo statement anywhere. Also, if that PHP code is really in your database like that, I believe you are on the wrong track.
Much better would be to use some sort of placeholder, and replace that by your desired database value when rendering that menu.
Something like this:
// assume something like "... <img src="../images/employees/%{image}"> ..."
// in the menu column, where '%{image}' serves as a placeholder
echo str_replace('%{image}', $row['image'], $row['menu']);
You need to echo your tag or echo your data inside the src attribute to use the value return by $row_UserDetails['Image'].
If you have done so, this may be the problem.
You have a issue with cocatenation. Your code should be like this
<?php echo "<img src='../images/employees/".$row_UserDetails['Image']."' alt='test'/>"; ?>
This article mat help you : https://teamtreehouse.com/community/why-do-we-use-concatenation-when-outputting-this-img-tag-with-php
Best regards !
I've been stuck on this all morning and I don't think i'm that much further.
I'm trying to display a wordpress menu, I then want to display all the child pages for the parent menu, all the child pages have thumbnails and I want these to show to.
I'me trying to add the thumbnails into the ing tag in the child pages
Here's how I went the code to look
Parent menu
<nav>
<a href="#" >Sample Page 1</a>
<a href="#" >Sample Page 2</a>
</nav>
Child menu
<ul class="sample_page_1">
<li>
<a href="#">
<img src="image.jpg" alt="img05">
<h4>Belts</h4>
</a>
</li>
<li>
<a href="#">
<img src="image.jpg" alt="img05">
<h4>Belts</h4>
</a>
</li>
</ul>
<ul class="sample_page_2">
<li>
<a href="#">
<img src="image.jpg" alt="img05">
<h4>Belts</h4>
</a>
</li>
<li>
<a href="#">
<img src="image.jpg" alt="img05">
<h4>Belts</h4>
</a>
</li>
</ul>
Here's the code I have so far but it's not doing what it should, it's not displaying the images and I can't figure out how? Not even sure it's it the correct way to do it?
<ul>
<?php
$menu_name = 'shoes';
$items = wp_get_nav_menu_items($menu_name);
foreach ( $items as $item){
echo '<li>'.$item->title.'</li>';
}
?>
</ul>
Thanks
Testing this on my end yielded satisfactory results. This will affect ALL menus, so feel free to add additional logic to only target certain menus.
You'll probably have to style it to make it look a bit better, but just drop this into your functions.php file:
add_filter('walker_nav_menu_start_el', 'maiorano_generate_nav_images', 20, 4);
function maiorano_generate_nav_images($item_output, $item, $depth, $args){
if(has_post_thumbnail($item->object_id)){
$dom = new DOMDocument(); //DOM Parser because RegEx is a terrible idea
$dom->loadHTML($item_output); //Load the markup provided by the original walker
$img = $dom->createDocumentFragment(); //Create arbitrary Element
$img->appendXML(get_the_post_thumbnail($item->object_id, 'thumbnail')); //Apply image data via string
$dom->getElementsByTagName('a')->item(0)->appendChild($img); //Append the image to the link
$item_output = $dom->saveHTML(); //Replace the original output
}
return $item_output;
}
To create an editable pane in C5 I use the following between div tags so the user can simply use the content editor to add text. This works quite well:
<div class="myWrapper">
<?php
$a = new Area('WelcomeText');
$a->display($c);
?>
</div>
But what do I do when the mark-up is a little complicated? I would like to get user to update the 2 images and respective links themselves. Eg picture: http://i48.tinypic.com/4jma8p.png
What is the easiest way for non-code literate users to do this?
<ul class="Gal_2">
<li class="Frst">
<a href="<?php echo $this->getThemePath(); ?>/Images/TEMP_IMG2.jpg" rel="group">
<img src="<?php echo $this->getThemePath(); ?>/Images/TEMP_IMG2.jpg" width="224" height="150" alt="Island Rab" align="left" />
</a>
</li>
<li>
<a href="<?php echo $this->getThemePath(); ?>/Images/TEMP_IMG1.jpg" rel="group">
<img src="<?php echo $this->getThemePath(); ?>/Images/TEMP_IMG1.jpg" width="224" height="150" alt="Island Rab - Lopar beach" align="right" />
</a>
</li>
</ul>
Thanks in advance...
PC
Create a custom block with the free Designer Content addon: http://www.concrete5.org/marketplace/addons/designer-content
The block you create will have two image fields that link to other pages, and then use "static html" fields to surround the images with your <ul> and <li> tags.
This is actually a perfect use case for Designer Content, so it should be fairly self-explanatory. But if you run into problems, post a message to the support forum (or just email me directly at concrete#jordanlev.com).
Okay, so what I am trying to achieve is a "News Feed" type of thing with PHP and a MySQL database. I have the database and tables setup, and a CMS setup that inserts info into the database, and have successfully called individual rows of the table into my HTML layout using the SELECT statement.
Now, what I am trying to do is have a home page, which contains 12 divs, each one being an image that links to an individual post page, and dynamically generated from my table in DESC order. That way, the 12 most recent designs are displayed on the homepage.
Then, I want to achieve the same type of thing except on a page that is sorted by category, which I am assuming would be done by adding a WHERE category=categoryname to my SELECT statement. Just hoping someone with more knowledge could give me some guidance on how to proceed with this.
Here's my HTML (I inserted Needs to be dynamically generated in the links and img src):
/* Other relevant Code */
<div class="homeContent">
<div class="newsItems">
<div class="newsItem1">
<img src="**Needs to be dynamically generated**" alt="" class="newsItem" />
</div>
<div class="newsItem2">
<img src="**Needs to be dynamically generated**" alt="" class="newsItem" />
</div>
<div class="newsItem3">
<img src="**Needs to be dynamically generated**" alt="" class="newsItem" />
</div>
<div class="newsItem4">
<img src="**Needs to be dynamically generated**" alt="" class="newsItem" />
</div>
<div class="newsItem5">
<img src="**Needs to be dynamically generated**" alt="" class="newsItem" />
</div>
<div class="newsItem6">
<img src="**Needs to be dynamically generated**" alt="" class="newsItem" />
</div>
<div class="newsItem7">
<img src="**Needs to be dynamically generated**" alt="" class="newsItem" />
</div>
<div class="newsItem8">
<img src="**Needs to be dynamically generated**" alt="" class="newsItem" />
</div>
<div class="newsItem9">
<img src="**Needs to be dynamically generated**" alt="" class="newsItem" />
</div>
<div class="newsItem10">
<img src="**Needs to be dynamically generated**" alt="" class="newsItem" />
</div>
<div class="newsItem11">
<img src="**Needs to be dynamically generated**" alt="" class="newsItem" />
</div>
<div class="newsItem12">
<img src="**Needs to be dynamically generated**" alt="" class="newsItem" />
</div>
</div>
</div>
I haven't gotten very far with the PHP for this part, as I am not sure which direction to go, and I have slowly learned that if you choose the wrong direction with PHP it usually involves starting over. Thanks in advance for any help!
Long Story Short, how can I get the img src and links to be dynamically generated into these predefined DIVs from a MySQL table using PHP, and get them to be in DESC order?
The following code will take the data from your database and display a new <div>, link, and image.
Image.php:
<?php
$query = "SELECT id, link, image FROM images ORDER BY id DESC";
?>
<html>
<body>
<?php while($row = mysql_fetch_assoc($query)): ?>
<div class="NewsItem<?php echo $row['id']; ?>">
<img src="<?php echo $row['image']; ?>" alt="" class="newsItem" />
</div>
<?php endwhile; ?>
</body>
</html>
This assumes you have a table with the following columns:
ID (AUTO INCREMENT) INT
image TINYTEXT - URL to image file
link TINYTEXT - Link to where you want the link to go.
This answer assumes you have the following:
A MySQL connection in PHP (here, this is stored in the $sql variable).
image_src, url, and created_at columns in your MySQL table (we'll call the table my_table).
Querying for Everything
First off, let's go over the query.
We want to get all the data for all twelve rows at the same time so we don't have to query multiple times. We also want everything ordered in a descending fashion, which is where created_at comes into play.
It could look something like this:
SELECT m.image_src, m.url, m.created_at
FROM my_table AS m
ORDER BY m.created_at DESC
LIMIT 12
Let's store this query in a variable. We'll make it simple and call it $query
$query = /* The query we just wrote above */
Displaying the Results
Now we just have to figure out how to make the result look nice. I'll assume you know how to run the query, seeing how you've managed it already.
Looking at your example, we want each result to be in it's own div element, with a unique class attribute. I would recommend making those into ids instead, since they will be unique. We can add a newsItem class to each one instead.
To do what we want, we'll create a while loop to iterate over each row and print out the appropriate HTML. It could look like this:
<div class="newsItems">
<?php
$result = $sql->query($query);
$count = 0;
while(null !== ($r = $result->fetch_assoc()): ?>
<div id="newsItem<?php echo ++$count; ?>" class="newsItem">
<img src="<?php echo $r['image_src']; ?>" alt="" />
</div>
<?php
endwhile;
?>
</div>
A Couple of Things
Take careful notice of the while condition. With new versions of PHP, it is important that you compare the $r variable against a null value. It used to be possible to use false instead, but now that it isn't, a statement like this one will throw an error:
while(false !== ($r = $result->fetch_assoc())):
Be careful with your statements.
Note how we only put the newsItem class on the div elements. Because the a and img tags are inside of the div, you can select either of them using .newsItem a or .newsItem img in your stylings.
If you have a series of unique classes, it is better to make them ids than classes for clarity. There is no point in having a class that only gets used once.
Enjoy and good luck with your project!
Hi I have created myself a portfolio website.On the homepage in the footer on the right I would like to add four images from the portfolio page that change every time the page is loaded.Can anyone tell me how this can be achived?
On the portfolio page this is the structure for the html:
<ul class="ourHolder">
<li class="item" data-id="id-1" data-type="websites" >
<img src="img/portfolio/websites/small/1.jpg" alt="">
<div class="full"></div>
</li>
</li>
<li class="item" data-id="id-2" data-type="pdesigns">
<img src="img/portfolio/Print/small/1.jpg" alt="" />
<div class="full"></div>
</li>
<li class="item" data-id="id-3" data-type="ldesign">
<img src="img/portfolio/Logo/small/001.jpg" alt="" />
<div class="full"></div>
</li>
....
</ul>
I am using the jquery quicksand plugin to create a nice effect.I am trying to randomnly get 4 images from this page and post them on the homepage in the footer.Here is my website:
foxteam.net
Based on the information you provided, here's what you can do
//Database connection here
$sql="
SELECT image_link FROM photos
ORDER BY RAND()
LIMIT 4";
//Executing the query here
while($row = $result->fetch()){
echo '<img src="', $row['image_link'], '" />';
}
Edit: Please note that ORDER BY RAND() has some performance issues with big tables, if your "gallery" table is under 100, then I don't think you should worry about it.