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;
Related
I have this piece of code regarding a carousel. I need the first card to have the "active" class but not the following ones or the carousel wont slide. How can i do that?
Here's my code...
$sql = "select wine.wine_name, wine.id, wine.wine_img,
region.region_name, winetype.winetype_name from wine, region, winetype
where wine.region_id = region.id and wine.winetype_id = winetype.id
and sponsored = 1;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<div class="carousel-item col-md-4 active">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src='.$row["wine_img"].' alt="Card image cap">
<div class="card-body">
<h4 class="card-title">'.$row["wine_name"].'</h4>
<p class="card-text">'.$row["winetype_name"].' - '.$row["region_name"].'</p>
</div>
</div>
</div>';
}
} else {
echo "ERRO!";
}
you can use a counter to set the active class.
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$count_results = 0;
while($row = $result->fetch_assoc()) {
echo '<div class="carousel-item col-md-4 ' . ($count_results == 0 ? 'active' : '') . '">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src='.$row["wine_img"].' alt="Card image cap">
<div class="card-body">
<h4 class="card-title">'.$row["wine_name"].'</h4>
<p class="card-text">'.$row["winetype_name"].' - '.$row["region_name"].'</p>
</div>
</div>
</div>';
$count_results++;
}
} else {
echo "ERRO!";
}
You could just have a field with the extra class part set to active and then reset it at the end of the loop...
// output data of each row
$class = ' active';
while($row = $result->fetch_assoc()) {
echo '<div class="carousel-item col-md-4' .$class. '">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src='.$row["wine_img"].' alt="Card image cap">
<div class="card-body">
<h4 class="card-title">'.$row["wine_name"].'</h4>
<p class="card-text">'.$row["winetype_name"].' - '.$row["region_name"].'</p>
</div>
</div>
</div>';
$class = '';
}
This should do the trick:
while($row = $result->fetch_assoc()) {
$active = isset($active) ? '' : 'active';
echo '<div class="carousel-item col-md-4 ' . $active . '">
...
}
The variable $active will only be unset (undefined or null) on the first iteration. Then, on the other iterations, the variable will be defined and not null (since it's an empty string) so then we simply set it as an empty string again.
I would make a counter and simply check if you need to add the class. Of course, since you only need it once, you can use a boolean.
<?php
$sql = "select wine.wine_name, wine.id, wine.wine_img,
region.region_name, winetype.winetype_name from wine, region, winetype
where wine.region_id = region.id and wine.winetype_id = winetype.id
and sponsored = 1;";
$result = $conn->query($sql);
?>
<?php if ($result->num_rows > 0): ?>
<?php $first = true; ?>
<?php while ($row = $result->fetch_assoc()): ?>
<div class="carousel-item col-md-4 <?= $first ? 'active' : '' ?>">
<div class="card align-items-center">
<img class="card-img-top img-fluid " src=<?= $row["wine_img"] ?> alt="Card image cap">
<div class="card-body">
<h4 class="card-title"><?= $row["wine_name"] ?></h4>
<p class="card-text"><?= $row["winetype_name"] ?> - <?= $row["region_name"] ?></p>
</div>
</div>
</div>
<?php $first = false; ?>
<? endwhile; ?>
<?php else: ?>
<span>Error!</span>
<?php endif; ?>
I've also taken the liberty of taking all of your HTML out of the single echo. This is much cleaner :)
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>';
}
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;
got stuck in extracting the correct month from timestamp...Here's my Code
$sql = "SELECT id,notice,description,DAY( `posted_date` ) AS DAY, MONTH(`posted_date` ) AS MONTH, YEAR( `posted_date`) AS YEAR, enable FROM n_notice";
$result=mysqli_query($con,$sql);
while ($run = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$notice = $run['notice'];
$description = $run['description'];
$day =$run['DAY'];
//$month=$run['MONTH'];
$month=date("M",$run['MONTH']);
$year=$run['YEAR'];
?>
<div class="media">
<div class="media-left media-middle">
<div id="date">
<div class="mon"><?php echo $month;?></div>
<div class="day"><?php echo $day;?></div>
<div class="year"><?php echo $year;?></div>
</div>
</div>
<div class="media-body">
<h5 class="media-heading"><?php echo $notice; ?></h5>
<?php echo $description; ?>
</div>
</div>
<?php
}
// Free result set
mysqli_free_result($result);
?>
</div>
DB timestamp column
How to get the correct month in "M" format? All the Months are shown as "Jan"!!!
$month=date("M",$run['MONTH']);
Try to change "M" to "F".
F - A full textual representation of a month (January through December)
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