How to add in this code multiple ID category.
I have in kategorija_id 5 arrays:
1 - News
2 - Magazine
3 - Kokursi
4 - Grantovi ect..
I whant to display just key 1 and 2
when i add array i just print the first key
$kategorija = Kategorija::model()->findByPk($item->kategorija_**id=1,2**);
Thnx :)
Code update:
<div class="carousel-inner">
<div class="item active">
<ul class="thumbnails">
<?php
$i = 0;
$lastDefault = 0;
foreach ($ids as $itemId) {
$item = InfoPaket::model()->findByPk($itemId);
$kategorija = Kategorija::model()->findByPk($item->kategorija_id);
if ( $i % 3 == 0 && $i > 0 ) {
echo "</ul></div><div class='item'><ul class='thumbnails'>";
}
$imageUrl = $_SERVER['DOCUMENT_ROOT'] . Yii::app()->baseUrl . "/images/infopaket/" . $item->slika;
if ( !file_exists($imageUrl) || empty($item->slika)) {
if ( $lastDefault == 0 ) {
$imagePath = Yii::app()->baseUrl . "/images/infopaket/info_paket.png";
$lastDefault = 1;
} else {
$imagePath = Yii::app()->baseUrl . "/images/infopaket/info_paket_redv2.png";
$lastDefault = 0;
}
} else {
$imagePath = Yii::app()->baseUrl . "/images/infopaket/" . $item->slika;
}
echo "<li class='span3 thmb-elem'>";
echo "<div class='thumbnail right-caption image'>";
echo "<a class='img-link' href='" . Yii::app()->getHomeUrl() . "?r=infoPaket/clanak&id=" . $item->id . "'><img class='img-class' src='$imagePath'></a>";
echo "</div>";
echo "<div class='caption caption-link'>";
echo "<div class='naziv'><img class='arrow-img' src='". Yii::app()->getBaseUrl() . "/themes/shadow_dancer/images/arrow.png'> " . strtoupper($kategorija->naziv) . "</div>";
echo "<p><a class='naslov' href='" . Yii::app()->getHomeUrl() . "?r=infoPaket/clanak&id=" . $item->id . "'><strong>" . $item->naslov . "</strong></a></p>";
echo "</div>";
echo "</li>";
$i++;
}
?>
</ul>
</div>
</div>
I solve this problem adding this line of code
if ($item->kategorija_id==3 || $item->kategorija_id==4 ) {
now code look like this and it works perfect.
<?php
$lastDefault = 0;
foreach ($ids as $itemId) {
$item = InfoPaket::model()->findByPk($itemId);
$kategorija = Kategorija::model()->findByPk($item->kategorija_id);
if ($item->kategorija_id==3 || $item->kategorija_id==4 ) {
echo '<li>';
echo '<img src="<?php echo Yii::app()->theme->baseUrl; ?>/images/infopaket/info_paket.png" />';
echo '<h2>';
echo strtoupper($kategorija->naziv);
echo '</h2>';
echo '<h1>';
echo "<a class='naslov' href='" . Yii::app()->getHomeUrl() . "?r=infoPaket/clanak&id=" . $item->id . "'>";
echo $item->naslov;
echo '</h1>';
echo '</a>';
echo '</li>';
}
}
?>
findByPk() accepts first argument as array too. So you can do
category::model()->findByPk(array('1', '2'));
Also, $item->category_id will return only single entry. If your item has multiple categories, than set up relations to categories and then get categories ID's with
CHtml::listData($item->categoriesRelationName, 'id', 'id');
can use function ($data){return $data->attribtues} too ---------^--- to get all attributes of model
Related
How can I display regions and their cities only with ads? And why I can't see numbers of ads? I would like use jQuery but I don't know how.
<?php
$aRegions = Region::newInstance()->getByCountry('AT');
if(count($aRegions) > 0 ) { ?>
<ul>
<?php
foreach($aRegions as $region) {
//print_r ($region);
echo "<li>";
//$region['pk_i_id'].
?>
<div class="accordionButton">
<a href="javascript:void()">
<?php echo $region['s_name']."\n"; ?>
</a>
<?php // echo "</em>(". $region['items'].")</em>";?>
</div>
<?php
$aCities = City::newInstance()-> getByRegion($region['pk_i_id']);
if(count($aCities) > 0 ) {
echo "<div class=\"accordionContent\">";
echo "<ul>";
foreach($aCities as $city) {
// print_r ($city);
//$city["pk_i_id"].'
echo "<li>";
echo "<a href='". osc_search_url( array( 'sRegion'=>$region["s_name"], 'sCity' => $city['s_name'] ) ) ."'> ";
echo $city["s_name"]."\n";
echo "</em>(". $city['items'].")</em>";
echo "</a>";
echo "</li>";
}
}
echo "</ul>";
echo "</li>";
} ?>
</ul>
<?php
}
?>
The solution for cities is:
<?php if(osc_count_list_cities() > 0 ) { ?>
<ul>
<?php while(osc_has_list_cities() ) { ?>
<li><?php echo osc_list_city_name() ; ?> <em>(<?php echo osc_list_city_items() ; ?>)</em></li>
<?php } ?>
</ul>
<?php } ?>
Now it's tested and workes for cities
This will list all cities and their number of items.
The solution for regions > cities is:
$locations = array();
if(osc_count_list_cities() > 0 ) {
while(osc_has_list_cities() ) {
$city_id = osc_list_city_id();
$city = City::newInstance()->findByPrimaryKey($city_id);
$region_id = $city['fk_i_region_id'];
$locations[$region_id][$city_id] = array("cityurl"=>osc_list_city_url(), "cityname"=>osc_list_city_name(), "cityitems"=>osc_list_city_items());
}
echo '<ul>';
while(osc_has_list_regions() ) {
$region_id = osc_list_region_id();
echo '<li>' . osc_list_region_name() . '<em>(' . osc_list_region_items() . ')</em>' ;
echo '<ul>';
foreach($locations[$region_id] as $acity) {
echo '<li>' . $acity['cityname'] . '<em>(' . $acity['cityitems'] . ')</em></li>' ;
}
echo '</ul></li><br/>';
}
echo '</ul>';
}
Thanks! Your code is good! I need to rewrite it now for country display. This is my code but does not work properly, instead of showing German regions this code display austrian regions again:
<?php
$locations = array();
if(osc_count_list_cities() > 0 ) {
while(osc_has_list_cities() ) {
$city_id = osc_list_city_id();
$city = City::newInstance()->findByPrimaryKey($city_id);
$region_id = $city['fk_i_region_id'];
$locations[$region_id][$city_id] = array("cityurl"=>osc_list_city_url(), "cityname"=>osc_list_city_name(), "cityitems"=>osc_list_city_items());
}
?>
<ul>
<?php while(osc_has_countries() ) { ?>
<li><?php echo osc_country_name() ; ?> <em>(<?php echo osc_country_items() ; ?>)</em>
<?php if (osc_country_name() =='Austria') { ?>
<?php
echo '<ul>';
while(osc_has_list_regions('AT') ) {
$region_id = osc_list_region_id();
echo '<li>' . osc_list_region_name() . '<em>(' . osc_list_region_items() . ')</em>' ;
echo '<ul>';
foreach($locations[$region_id] as $acity) {
echo '<li>' . $acity['cityname'] . '<em>(' . $acity['cityitems'] . ')</em></li>' ;
}
echo '</ul></li><br/>';
}
echo '</ul>';
}
if (osc_country_name() =='Germany') { ?>
<?php
echo '<ul>';
while(osc_has_list_regions('DE') ) {
$region_id = osc_list_region_id();
echo '<li>' . osc_list_region_name() . '<em>(' . osc_list_region_items() . ')</em>' ;
echo '<ul>';
foreach($locations[$region_id] as $acity) {
echo '<li>' . $acity['cityname'] . '<em>(' . $acity['cityitems'] . ')</em></li>' ;
}
echo '</ul></li><br/>';
}
echo '</ul>';
}
}
} ?>
The solution for COUNTRIES > REGIONS > CITIES in Osclass 3.7.x is
$locations = array();
if(osc_count_list_cities() > 0 ) {
while(osc_has_list_cities() ) {
$city_id = osc_list_city_id();
$city = City::newInstance()->findByPrimaryKey($city_id);
$region_id = $city['fk_i_region_id'];
$country_code = strtolower($city['fk_c_country_code']);
$locations[$country_code][$region_id][$city_id] = array("cityurl"=>osc_list_city_url(), "cityname"=>osc_list_city_name(), "cityitems"=>osc_list_city_items());
}
$locationsRegions = array();
while(osc_has_list_regions() ) {
$region_id = osc_list_region_id();
$region = Region::newInstance()->findByPrimaryKey($region_id);
$country_code = strtolower($region['fk_c_country_code']);
$locationsRegions[$country_code][$region_id] = array("regionurl"=>osc_list_region_url(), "regionname"=>osc_list_region_name(), "regionitems"=>osc_list_region_items());
}
echo "<ul>";
while(osc_has_list_countries() ) {
$country_code = strtolower(osc_list_country_code());
echo '<li>' . osc_list_country_name() . '<em>(' . osc_list_country_items() . ')</em>' ;
echo '<ul>';
foreach($locationsRegions[$country_code] as $regionId => $aregion) {
echo '<li>';
echo '' . $aregion['regionname'] . '<em>(' . $aregion['regionitems'] . ')</em></br>' ;
echo '<ul>';
foreach($locations[$country_code][$regionId] as $acity) {
echo '<li>' . $acity['cityname'] . '<em>(' . $acity['cityitems'] . ')</em></li>' ;
}
echo '</ul></li><br/>';
}
echo '</ul></li><br/>';
}
echo '</ul>';
}
if (isset($_GET['q'])){
$q = $_GET['q'];
//variables
$sql = "SELECT * FROM products WHERE product LIKE '%$q%' OR search LIKE '%$q%'";
$result = $conn->query($sql);
//check database
if($result->num_rows > 0){
while ($row = $result->fetch_array()){
$product = $row['product'];
$productImage = $row['product_image'];
$price = $row['price'];
$seller = $row['seller_name'];
$sellerImage = $row['seller_image'];
$desc = $row['description'];
$search = $row['search'];
$console = $row['console'];
$array = array($product, $price, $productImage);
$arrayDesc = array($desc, $sellerImage);
if (preg_match('/Game/', $seller)){
for ($num = 0; $num < 3; $num++) {
echo '<div class="tile col-md-4 col-sm-3">';
if($num == 0){
echo '<div class="tileTitleBox"><h4>' . $array[$num] . '</h4></div>';
}
if($num = 1){
echo '<p class="price">' . $array[$num] . '</p>';
}
if($num = 2){
echo '<img class="tilePic" src="' . $array[$num] . '"/>';
}
// if($num = 3){
// echo '<div class = "desc"><p>' . $array[$num] . '</p></div>';
// }
echo '</div>';
}//For Iteration Loop - TILE
for($count = 0; $count < 2; $count++){
echo '<div class="tile-description col-md-4 col-sm-3 hidden">';
if($count == 0){
echo '<div class="desc"><p>' . $arrayDesc[$count] . '</p></div>';
}
if($count == 1){
echo '<img class="sellerImg" src="' . $arrayDesc[$count] . '"/>';
}
echo '</div>';
}//end for loop - TILE-DESCRIPTION
So as you see above, I have one for loop which creates the "tile" and the second which is supposed to create the "tile-description". The first one works well to create a single div with the class of "tile" in which my remaining contents is loaded into the div. But in the second for loop the "desc" dev and the "sellerImg" div are separated. The loop needs to go through the array various times.
Shown Bellow:
You can see that the "Tile" div contains "tileTitleBox", "price", and "tilePic". But the "tile-description" which is hidden, is separated into two divs. As opposed to holding both elements inside the same div.
#sam-dufel rightly pointed out you're overwriting the iterator
if($num = 1){
But in fact you shouldn't use any loops here:
echo '<div class="tile col-md-4 col-sm-3">';
echo '<div class="tileTitleBox"><h4>' . $array[0] . '</h4></div>';
echo '<p class="price">' . $array[1] . '</p>';
echo '<img class="tilePic" src="' . $array[2] . '"/>';
echo '</div>';
echo '<div class="tile-description col-md-4 col-sm-3 hidden">';
echo '<div class="desc"><p>' . $arrayDesc[0] . '</p></div>';
echo '<img class="sellerImg" src="' . $arrayDesc[1] . '"/>';
echo '</div>';
As you can see on http://www.mattmaclennan.co.uk/a2 ... when you hover over "New Motorcycles", it has the array numbers, and not the labels as required. Also, the categories need to be grouped into their IDs. Below is what I have tried.
<?
$output = mysqli_query("SELECT * FROM bikes, bikeTypes WHERE bikes.model_id = bikeTypes.model_id");
$result = array();
while($row = mysqli_fetch_array($output))
{
$result[] = $row;
}
//var_dump($result);
foreach ($result as $key => $val) {
echo "<li><a href='test.php?id=" . $val['model_id'] . "'>".$key.'</a><ul>';
echo "<li><a href='details.php?id=" . $val['bike_id'] . "'>" . $val['bikeName'] . "</a></li>";
echo '</ul>';
echo '</li>';
}
?>
The category field is called 'model'. Thanks for any help!
Thats because you're display the $key, instead of the $value.
<?
$output = mysqli_query("SELECT * FROM bikes, bikeTypes WHERE bikes.model_id = bikeTypes.model_id");
while($row = mysqli_fetch_array($output))
{
echo "<li><a href='test.php?id=" . $row['model_id'] . "'>".$row['???'].'</a><ul>';
echo "<li><a href='details.php?id=" . $row['bike_id'] . "'>" . $row['bikeName'] . "</a></li>";
echo '</ul>';
echo '</li>';
}
?>
I don't know PHP all that well so I hope I will be showing enough code when I ask this question. I have a part of my homepage that is going to show the latest 5 blog posts and so I set it up like this:
<?php
function get_latest_post_html() {
$content = "";
query_posts('showposts=5');
while (have_posts()){
the_post();
$content .= "<p class='title'><a href='" . get_permalink() . "'>" . get_the_title() . "</a></p>\n" .
"<p class='excerpt'><a href='" . get_permalink() . "'><img src='" . wp_get_attachment_url( get_post_thumbnail_id($post->ID) ) . "' class='rt-image img-left wp-post-image' style='max-width:175px;'/></a>" . get_the_excerpt() . "</p><br/><hr/>";
}
wp_reset_query();
return "<div class='latest-post'>\n$content\n</div>";
}
add_shortcode('get_latest_post', 'get_latest_post_html');
?>
It calls the last 5 posts just fine, but I don't want to have it display the <hr/> on the bottom of the 5th post.
Setup some logic in your while loop to conditionally display the <hr >.
For example:
$i = 0;
while (have_posts()) {
++$i;
the_post();
// ...
if ($i < 5) {
$content .= '<hr />';
}
}
Note: WordPress may not return 5 posts, so you should consider that path. I would also discourage string concatenation in tight loops. Refactor your code and use echo.
Since you only need to get rid of the last <hr/>.
Try use substr()
So in your case, add this after while loop ends
$content = substr($content, 0, -5)
<?php
function get_latest_post_html() {
$content = "";
query_posts('showposts=5');
$i = 0;
while (have_posts()){
i++;
if(i < 5){
the_post();
$content .= "<p class='title'><a href='" . get_permalink() . "'>" . get_the_title() . "</a></p>\n" .
"<p class='excerpt'><a href='" . get_permalink() . "'><img src='" . wp_get_attachment_url( get_post_thumbnail_id($post->ID) ) . "' class='rt-image img-left wp-post-image' style='max-width:175px;'/></a>" . get_the_excerpt() . "</p><br/><hr/>";
}
else{
$i = 0;
//do something
}
}
wp_reset_query();
return "<div class='latest-post'>\n$content\n</div>";
}
add_shortcode('get_latest_post', 'get_latest_post_html');
?>
I have a pretty complicated script that doesn't fully work with MySQL, it does partially though. Let me try to explain...
The results of my page are purely image names from a specific folder, means I use this function to get my results:
function get_all_images($dir)
{
$dir = opendir($dir);
$dirArray = array();
while($entryName = readdir($dir))
{
if(($entryName != ".") && ($entryName != "..") && ($entryName != ".svn") && ($entryName != ".htaccess"))
{
$dirArray[] = $entryName;
}
}
closedir($dir);
(sizeof($dirArray)) ? arsort($dirArray) : '';
return (is_array($dirArray)) ? $dirArray : '';
}
This is how I basically get results in my page:
<?php
include('includes/header.php');
$images = get_all_images('i');
$url = str_replace('www.', '', generate_site_url());
$flag = false;
$count = 0;
if (empty($images))
{
echo '<h2>There are no uploaded images</h2><br>';
}
foreach ($images as $image)
{
$filename = $image_name = $image;
$image_link = $url . IMAGES_PATH . $filename;
$user_id = fetch_user_id($image_link);
$delete_link = (isset($_POST['delete_link'])) ? $_POST['delete_link'] : '';
$delete_image = (isset($_POST['delete_image'])) ? $_POST['delete_image'] : '';
if ($delete_admin_submit)
{
unlink('./t/' . $delete_image);
unlink('./t/big' . $delete_image);
adminDelete('./i/' . $delete_image, $delete_link);
header('Location: ' . $imgit_action);
exit();
}
echo '<div class="' . ($count++ % 2 ? "odd-color" : "even-color") . '">';
echo '<table>';
echo '<tr><td class="fullwidth"><a class="preview_img" href="' . $image_link . '"><img src="' . $image_link . '" title="Click to enlarge" width="300" class="thumb" /></a></td></tr>';
echo '<tr><td><span class="default">Direct link:</span> ';
echo '<input type="text" readonly="readonly" class="link-area" onmouseover="this.select();" value="' . $image_link . '" />';
echo '<form method="post" action="" onsubmit="return confirmSingleDeletion();" style="display: inline;"> ';
echo '<input type="submit" class="icon_delete" name="delete_link" value="' . $image_link . '" title="Delete this image" />';
echo '<input type="hidden" name="delete_image" value="' . $image_name . '" />';
echo '</form>';
echo '</td></tr>';
echo ($flag) ? '<hr /><br>' : '';
echo '</table>';
if (!empty($user_id))
{
echo '<br><strong class="normal">Uploader ID:</strong> ';
echo '<em class="normal">' . $user_id . '</em><br>';
}
echo '<br>';
$flag = true;
}
?>
<span class="button-sub">« Back to Index</span>
<?php echo '</div>'; ?>
<?php include('includes/footer_alt.php'); ?>
Now I have not ANY simple clue how to start breaking my results into pages. I'm working here with over 12000 results and it takes a lot for the page to load, I need help to break this big result into pages.
Anyone willing to help me? At least give me a clue how to start? I would be really grateful.
Thanks a lot for reading.
Consider your array as you collect up the file names but after you have sorted them:
$imgs = array(
0 => 'image1.jpg',
1 => 'image2.jpg',
2 => 'image3.jpg',
3 => 'image4.jpg',
4 => 'image5.jpg',
5 => 'image6.jpg',
6 => 'image7.jpg',
7 => 'image8.jpg',
);
// create some vars which you can use in your pagination
$perpage = 3 ;
$page=2;
$range_end = ($perpage*$page)-1;
$range_start = ($perpage*($page-1));
$display=range($range_start,$range_end);
// loop through results
foreach($display as $show){
echo $imgs[$show];
}
Does that get you a start?
Thanks for trying to answer me Cups and Umair Khan, but I found the working solution here:
http://tiffanybbrown.com/2008/12/14/simple-pagination-for-arrays-with-php-5/