Assign looped records to single variable using php - 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;

Related

How can i include php script while fetching data from database

I have stored comma separated value in database. Now I want to display list of that value. Currently I am trying to print "bni_member_export_to". It contain string like usa,uk,india. I need as list.
while($row = mysqli_fetch_array($result))
{
$output .= '
<br>
<hr>
<br>
<div class="row">
<div class="col-md-6">
<h5>Personal Information</h5>
<li>Name : '.$row["bni_member_name"].'</li>
<li>Mobile: '.$row["bni_member_mobile"].'</li>
<li>Email: '.$row["bni_member_email"].'</li>
</div>
<div class="col-md-6">
<h5>Business Information</h5>
<li>Chapter: '.$row["bni_chapter_id"].'</li>
<li>Category: '.$row["bni_category_id"].'</li>
</div>
</div>
<br>
<hr>
<br>
<div class="row">
<div class="col-md-12">
<h6>Description</h6>
<h7>'.$row["bni_member_bio"].'</h7>
</div>
</div>
<br>
<hr>
<br>
<div class="row">
<div class="col-md-4">
<li>Export to</li>
<li>'.$row["bni_member_export_to"].'</li>
</div>
<div class="col-md-4">
<li>Import From</li>
<li>'.$row["bni_member_import_from"].'</li>
</div>
<div class="col-md-4">
<li>Want to Connect To</li>
<li>'.$row["bni_member_want_to_connect_to"].'</li>
</div>
</div>
I have php code for list all string. But I doesn't work if I put it into
<li>Export to</li>
<li>'.$row["bni_member_export_to"].'</li>
</div>
for print list
$exp = $row["bni_member_export_to"];
$expl = explode(',', $exp);
foreach($expl as $my_Array){
echo $my_Array.'<br>';
}
You will have to stop the string concatenation, and start the PHP processor to generate the explode() and a foreach loop to generate the <li> items
$output .= '
. . .
<div class="col-md-4">
<ul> <!-- added the missing <ul> tag -->
<li>Want to Connect To</li>'; // not sure this <li> belongs here
<?php
$items = explode(',', $row["bni_member_want_to_connect_to"])
foreach ($items as $item) {
$output .= "<li>$item</li>";
}
?>
$output .= '</ul></div>';
You have some messed html (missing ul, ...)
But if you need html list from comma separated string ("usa,india"), you can explode your string into the array and then implode it back separated by </li><li>
'<ul><li>'.implode('</li><li>', explode(',', $row["bni_member_export_to"])).'<li></ul>'
or you can just replace comma with </li><li>
'<ul><li>'.str_replace(',', '</li><li>', $row["bni_member_export_to"])).'<li></ul>'
But I think your database is badly designed.
This should work
$exp = $row["bni_member_export_to"];
$expl = explode(',', $exp);
$output .= '<ul>';
foreach($expl as $my_Array){
$output .= '<li>'.$my_Array.'</li>';
}
$output .= '</ul>';
echo $output;

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>';
}

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

How to format date in PHP from phpMyAdmin database

I can't figure out how to format the timestamp on my database so I figured I should use my PHP code, but still cannot figure it out.
PHP CODE:
function filldiv() {
$loopResult .= '';
$events = mysql_query('SELECT * FROM a3825952_blog.Blog ORDER BY DATE DESC');
while($row = mysql_fetch_array($events)) {
$loopResult .= '
<div class="blogbox show">
<div class="blogtitle">'.$row['TITLE'].'</div>
<div class="blogdate">'.$row['DATE'].'</div>
<div class="blogcontent">'.$row['CONTENT'].'</div>
<div class="blogimage"> <img src="../'.$row['IMAGE'].'"/></div>
<div class="blogimage"> <img src="../'.$row['IMAGEB'].'"/></div>
<div class="blogimage"> <img src="../'.$row['IMAGEC'].'"/></div>
<div class="showHide"></div>
</div>
';
}
echo $loopResult;
Thanks for every ones suggestions on reading the documentation. For some odd reason I just didn't think of it. I was able to figure it out and posted the solution below. Again, thanks!
First I had to set the rows date to a variable, then format the date in the loop.
function filldiv() {
$loopResult .= '';
$events = mysql_query('SELECT * FROM database.table ORDER BY DATE DESC');
while($row = mysql_fetch_array($events)) {
$date = date_create($row['DATE']);
$loopResult .= '
<div class="blogbox show">
<div class="blogtitle">'.$row['TITLE'].'</div>
<div class="blogdate">'.date_format($date, 'l, F j, Y').'</div>
<div class="blogcontent">'.$row['CONTENT'].'</div>
<div class="blogimage"> <img src="../'.$row['IMAGE'].'"/></div>
<div class="blogimage"> <img src="../'.$row['IMAGEB'].'"/></div>
<div class="blogimage"> <img src="../'.$row['IMAGEC'].'"/></div>
<div class="showHide"></div>
</div>
';
}
echo $loopResult;

HOWTO: get data from MYSQL database and then use/show this data in a php page in a while/foreach?

I figured out how to get the data, how to use it in a while loop, but I am stuck on the last part: a loop in a loop.
I have the following code:
<div id="tab1" class="tab_content">
<ul class="columns">
<?php
$result = mysql_query("SELECT * FROM TabContent WHERE TabID = 1");
while($row = mysql_fetch_array($result))
{
echo '<li><img src="images/layouts/'. $row['LayoutName'] .'.png" alt="" />
<div class="info">
<h2>'. $row['LayoutName'] .'</h2>
<p>'. $row['LayoutTiles'] . 'Tiles, '. $row['LayoutLayers'] . 'Layers</p>
</div>
</li>';
}
?>
</ul>
<div class="clear"></div>
<div class="bottom">
<div class="clearfix">
<div class="lfloat"></div>
<div class="rfloat"><a class="hide" href="#" onclick="return false">Cancel</a></div>
</div>
</div>
</div>
<div id="tab2" class="tab_content">
etc etc same as above
The problem I have is with the div with id="tab1" and the next one with id="tab2" etc. I can get an array of tabs using:
<?php
$result = mysql_query("SELECT * FROM Tabs");
while($row = mysql_fetch_array($result))
{
echo 'tab'. $row['TabID'] .;
}
?>
Giving me: Tab1 Tab2 Tab3 etc. Now the hard part, how do I use these so I can make a loop using this to replace my html div ids?
Here's a couple of tutorials that might help you in your struggle:
http://www.freewebmasterhelp.com/tutorials/phpmysql
http://www.homeandlearn.co.uk/php/php.html

Categories