Yii: Getting name and image using cdbciteria - php

I want to retrieve data from my database using CDbcriteria. I want to get the name and image of the last 4 Ngos from my Ngo table.
<?php
$Criteria = new CDbCriteria();
$Criteria->limit = 4;
$Criteria->order = "id DESC";
$Criteria->select = "id, ngo_name, image";
$Ngos =Ngo::model()->findAll($Criteria);
?>
<div class="row">
<h3>NGO's</h3>
<div class="col-md-3">
<div class="thumbnail">
<img src="<?php
foreach ( (array)$Ngos as $Ngo)
{
echo '$Ngo->image';
}
?>"
<div class="caption">
<h3><?php
foreach ( (array)$Ngos as $Ngo)
{
echo '$Ngo->ngo_name';
}
?></h3>
<button class="btn btn-primary center-block">View Profile</button>
</div>
</div>
</div>

just remove single quotes around the variable $Ngo->image and $Ngo->ngo_name or use double quotes instead.

Related

After 500 results displayed from DB html/css loses formatting

I have a page that runs off a local webserver that is uses SQLite as its database. As its used local I am not worried about listing all results on one page as they load super fast. I am having an issue with it though as after 500 results are displayed from SQLite3 the formatting goes all wonky and starts stacking them on top of each other. Everything before that is fine. Its written in php. Info was entered into the database using htmlspecialchars so I dont believe that is the issue. The code that builds each record in the loop is
$list = '';
while($row = $results->fetchArray()) {
$id = $row["id"];
$MovieTitle = $row["MovieTitle"];
$MovieYear = $row["MovieDate"];
$MovieRes = $row["MovieRes"];
$FileName = $row["FileName"];
$Summary = $row["Summary"];
$Genres = $row["Genres"];
$PictureLocation = $row["PictureLocation"];
$Rating = $row["Rating"];
$ReleaseDate = $row["ReleaseDate"];
$list .= '<div class="box">
<div class="movie">
<div class="movie-image"><span class="play"><span class="name">'.$MovieTitle.'</span></span><img src="'.$ThumbnailPic.'" alt=""></div>
<div class="rating">
<p>RATING: '.$Rating.'</p>
<div class="stars">
<div class="'.$StarGraphic.'"></div>
</div>
<span class="comments"></span></div>
</div>';
}
and i just echo them them in the html as such
<html>
<body>
<div id="main">
<br>
<?php echo $list; ?>
</div>
</body>
</html>
Your HTML is wrong, you did not close <div class="box"> and <span class="play"> tags properly.
Correct HTML is:
<div class="box">
<div class="movie">
<div class="movie-image">
<span class="play">
<a href="movielist.php?movie='.$FileName.'">
<span class="name">'.$MovieTitle.'</span>
<img src="'.$ThumbnailPic.'" alt="">
</a>
</span>
</div>
<div class="rating">
<p>
RATING: '.$Rating.'
</p>
<div class="stars">
<div class="'.$StarGraphic.'"></div>
</div>
<span class="comments"></span>
</div>
</div>
</div>
Aso, you can have some tags or quotes in your database records. So you have to use escaping your variables before output http://php.net/manual/en/function.htmlspecialchars.php
Something like this:
$list = '';
while($row = $results->fetchArray()) {
$id = htmlspecialchars($row["id"]);
$MovieTitle = htmlspecialchars($row["MovieTitle"]);
$MovieYear = htmlspecialchars($row["MovieDate"]);
$MovieRes = htmlspecialchars($row["MovieRes"]);
$FileName = htmlspecialchars($row["FileName"]);
$Summary = htmlspecialchars($row["Summary"]);
$Genres = htmlspecialchars($row["Genres"]);
$PictureLocation = htmlspecialchars($row["PictureLocation"]);
$Rating = htmlspecialchars($row["Rating"]);
$ReleaseDate = htmlspecialchars($row["ReleaseDate"]);
$list .= '<div class="box">
<div class="movie">
<div class="movie-image"><span class="play"><span class="name">'.$MovieTitle.'</span></span><img src="'.$ThumbnailPic.'" alt=""></div>
<div class="rating">
<p>RATING: '.$Rating.'</p>
<div class="stars">
<div class="'.$StarGraphic.'"></div>
</div>
<span class="comments"></span></div>
</div>';
}

PHP ForEach Loop With Bootstrap prints list of 50 (all the same) where only 6 exist in DB

This is some Bootstrap HTML where I need to create multiple blocks like a table but it just grabs the first item in the DB and prints 50 of them. I'm having trouble figuring out how to mingle the php code and html code so it works correctly.
<section id="latest-news" class="latest-news-section">
<div class="container">
<?php
include_once ('mysql.inc.php');
$ix = $_POST['i'];
$sql = "SELECT * from deposits WHERE d_userid = '$ix' ORDER BY _productionid ASC";
$query = #mysql_query($sql);
$outcome = #mysql_fetch_array($query);
foreach ($outcome as $result1){
?>
This part comes out nicely formatted, but the only data is the first item in the DB, and there are 50 copies made
<div class="col-md-4 col-sm-4">
<div class="latest-post">
<img src="assets/images/<?php echo $outcome['d_picture'] ?>" class="img-responsive" alt="">
<h4>Your ST1 <?php echo $outcome['d_firstname'] ?></h4>
<p>Prod ID <?php echo $outcome['d_productionid'] ?></p>
<p>Color: <?php echo $outcome['d_color'] ?><br />
Equip: <?php echo $outcome['d_equip'] ?><br />
Custom: <?php echo $outcome['d_custom'] ?></p>
<a class="btn btn-primary">View More</a>
</div>
</div>
<?
}
#mysql_close;
?>
</div>
</section>
You should use $result1 and not $outcome
ex: $result1['d_picture']

Assign looped records to single variable using php

I need to get all looped records to single variable. I tried this but i got final records of loop. How do i get all records. Can you please solve this. Here i added my code,
$text = '';
$sql = mysql_query("SELECT * FROM table WHERE column_name = 'column_value'");
while($row = mysql_fetch_array($sql)){
$text = '<li><div class="row">
<div class="col-md-12 post">
<div class="row">
<div class="col-md-12">
<h5>
<strong>'.$row['field2'].'</strong></h4>
</div>
</div>
<div class="row post-content">
<div class="col-md-12">
<p style="margin-bottom:0px">
';
$text.= substr($row['field3'],0,150);
$text.= '...</p>
<a class="pull-right" href="'.$row['field2'].'.php">>> Read more</a>
</div>
</div>
</div>
</div></li>';
}
you are not concating your $text variable
$text = '';
while() { ...
$text .= '<li>...'; // you missed this dot
}
echo $text;

Take value from array and select it in mysql php

I have problem that I store in DB column (video_id) and the value of it like that store (1,3,4,7) the number mean the id of video in the Video table which store the video title and text and all information about the video but when I wrote code to select all video the result give me one video
<div class="panel-body">
<?
$qu="SELECT video_id FROM `training_questions` WHERE id=$questions_id AND category_id=$training_id";
$query_c= mysqli_query($con, $qu);
while ($row7 = mysqli_fetch_assoc($query_c)) {
$vedoes=$row['video_id'];
$pieces = explode(",", $vedoes);
}
for($i=0;$i<=count($pieces);$i++){
$query = "SELECT * FROM `video` WHERE id IN $pieces[$i] ";
$query_che= mysqli_query($con, $query);
while ($row2 = mysqli_fetch_assoc($query_che)) { ?>
<div class="col-lg-3 col-sm-6">
<div class="thumbnail">
<div class="video-container">
<div class="thumb"> <img src="../upload/<?= $row2['img'] ?>" class="img-responsive img-rounded media-preview" alt=""> <span class="zoom-image"><i class="icon-play3"></i></span> </div>
</div>
<div class="caption">
<h6 class="no-margin">
<a href ="video_details.php?id=<?= $row2['id'] ?>" class="text-default"><?= $row2['title'] ?>
</a>
</h6>
</div>
</div>
</div>
<?
}
} ?>
</div>
How I can fix it?
Remove the
$pieces = explode(",", $vedoes);
its useless
make your for loop condition look like this
for($i=0;$i<=count($vedoes);$i++)
Hope it Helps

Yii: Retrieving data using Cdbcriteria

I am yiibie. What I am trying to do is to retrieve data from my database, and for that i am using cDbCriteria. I want to get the last 4 images from my database table which is named Event. and then want to display those 4 images individually. I am using this method but not getting any result, please help me with this.
<?php
$Criteria = new CDbCriteria();
$Criteria->limit = 4;
$Criteria->order = "image DESC";
$Criteria->select = "id, image";
$Events = Event::model()->findAll($Criteria);
foreach ( (array)$Events as $Event)
{
$Event[1]=$image1;
$Event[2]=$image2;
$Event[3]=$image3;
$Event[4]=$image4;
}
?>
<div class="row">
<h3>Events</h3>
<div class="col-md-3">
<div class="thumbnail">
<img src="<?php echo $image1 ?>">
<div class="caption">
<button class="btn btn-primary center-block">Join</button>
</div>
</div>
</div>
findAll() method retrieves an array of ActiveRecords, so you need a foreach loop. Try:
foreach ( (array)$Events as $Event)
{
echo "
<div class='col-md-3'>
<div class='thumbnail'>
<img src='$Event->image' >
<div class='caption'>
<a href='join.php'><button class='btn btn-primary center-block'>Join</button></a>
</div>
</div>
</div>
";
}
$Criteria = new CDbCriteria();
$Criteria->limit = 4;
$Criteria->order = "id DESC";
$Criteria->select = "id, image";
$Events= Event::model()->findAll($Criteria);
This will gives last 4 records
1st way
if(isset($Events[0]))
echo $Events[0]->image;
if(isset($Events[1]))
echo $Events[1]->image;
if(isset($Events[2]))
echo $Events[2]->image;
if(isset($Events[3]))
echo $Events[3]->image;
i am here checking isset() because there may b 3 entry. This is static
2nd way
foreach($Events as $event)
{
echo $event->image;
}
this is dynamic binding.

Categories