I have some sort of timeline on my page, and I am getting 3 late news from database like that:
<div id="timeline">
<?php
$SQLGetNews = $odb -> query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 3");
while ($getInfo = $SQLGetNews -> fetch(PDO::FETCH_ASSOC)) {
$name = $getInfo['name'];
/*It should be like that
<div class="left">'.$name.'</div>
<div class="right">'.$name.'</div>
I know that i have to echo it somehow by targeting first, second, third output?
*/
}
?>
</div>
So you want to print info like left then right then left aligned, You can do this like below.
<div id="timeline">
<?php
$SQLGetNews = $odb -> query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 3");
$i=1;
while ($getInfo = $SQLGetNews -> fetch(PDO::FETCH_ASSOC)) {
$name = $getInfo['name'];
if($i%2)
{
echo '<div class="right">'.$name.'</div>';
}
else
{
echo '<div class="left">'.$name.'</div>';
}
$i++;
/*It should be like that
<div class="left">'.$name.'</div>
<div class="right">'.$name.'</div>
I know that i have to echo it somehow by targeting first, second, third output?
*/
}
?>
</div>
This should illustrate something you could do to alternate between left and right while iterating.
function isEven(int $num):bool{
return $num % 2 == 0;
}
$len = count($names = ['foo','bar','fez']);
$i = 0;
while($i < $len){
$name = $names[$i];
$class = "right";
if(isEven($i)){
$class = "left";
}
echo $name . " is on the " . $class . "\n";
$i++;
}
// foo is on the left
// bar is on the right
// fez is on the left
<div id="timeline">
<?php
$SQLGetNews = $odb -> query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 3");
$i=1;
while ($getInfo = $SQLGetNews -> fetch(PDO::FETCH_ASSOC)) {
$name = $getInfo['name'];
if($i%2)
{
?>
<div class="right"><?php echo $name; ?></div>
<?php
}
else
{
?>
<div class="left"><?php echo $name; ?></div>
<?php
}
$i++;
/*It should be like that
<div class="left">'.$name.'</div>
<div class="right">'.$name.'</div>
I know that i have to echo it somehow by targeting first, second, third output?
*/
}
?>
</div>
Related
<?php
$sqlquerypmenu = "select *
from subsubmenu
where submenu_id=1
and position='left'
and status=1";
if($querypmenu = sqlsrv_query($conn,$sqlquerypmenu)){
if(sqlsrv_has_rows($querypmenu) === true){
while($rowdata = sqlsrv_fetch_array($querypmenu, SQLSRV_FETCH_ASSOC)){
?>
<h4 class="title-small folder_name"> <?php echo $rowdata ['website_title']; ?> </h4>
<?php
$id = $rowdata['id'];
$filequerymenu = "select *
from upload_files
where main_menu='value_name'
and sub_menu='value_key'
and subsub_menu= $id ";
if($filemenu = sqlsrv_query($conn,$filequerymenu)){
if(sqlsrv_has_rows($filemenu) === true){
while($filedata = sqlsrv_fetch_array($filemenu, SQLSRV_FETCH_ASSOC)){ ?>
<a class="smalltext font_val" href="<?php echo DOCUMENT_URL.$filedata ['file_name']; ?> " target="_blank" ><?php echo $filedata['document_name']; ?></a>
<?php
}
}
}
}
}
}
?>
In the nested while loop the second while loop only shows the first row of data and does not show the rest of the data.
How can I fix this?
Consider querying the database once with one JOIN query. Possibly opening up another fetch within a while loop causes instances issues:
<?php
...
$sql = "select s.website_title, u.file_name, u.document_name
from upload_files u
inner join subsubmenu s ON u.subsub_menu = s.id
where u.main_menu = 'value_name'
and u.sub_menu = 'value_key'
and s.submenu_id = 1
and s.[position] = 'left'
and s.[status] = 1
order by s.id, s.website_title;"
$title = "";
if($result = sqlsrv_query($conn, $sql)){
if(sqlsrv_has_rows($result) === true){
while($rowdata = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
if($title != $rowdata['website_title']) {
$title = $rowdata['website_title']
?>
<h4 class="title-small folder_name"> <?php echo $rowdata ['website_title']; ?> </h4>
<?php
}
?>
<a class="smalltext font_val" href="<?php echo DOCUMENT_URL.$filedata ['file_name']; ?> " target="_blank" ><?php echo $filedata['document_name']; ?></a>
<?php
}
}
}
?>
I'm in need of some help. How can I output the nl2br() and htmlentities() functions into my code? Both need to be applied to 'content' but htmlentities to 'title'.
<?php
$sql = "
SELECT
post.id AS postid, post.title AS title, post.created,
DATE_FORMAT(Created, '%d-%m-%Y') AS created,
post.content AS content, post.image AS image,
post.alttext AS alttext, blog.id, blog.blogname
FROM
post
LEFT JOIN
blog ON post.blogid = blog.id
WHERE
blogid = \"" .(int)$_GET['id'] . "\"
ORDER BY
postid DESC";
$results = $db->query($sql);
if ($results->num_rows) {
while ($row = $results->fetch_object()) {
echo "<hr>
<h3>{$row->title}</h3>
<p>{$row->created}</p>
<p>
<div id='blog-image'>
<img src='images/{$row->image}' alt='{$row->alttext}'>
</div>
{$row->content}
</p>";
}
} else {
echo 'No Results';
}
?>
You can use a new variable for that:
if ($results->num_rows) {
while ($row = $results->fetch_object()) {
$title = htmlentities($row->title);
$content = nl2br(htmlentities($row->content));
echo "<hr>
<h3>{$title}</h3>
<p>{$row->created}</p>
<p>
<div id='blog-image'>
<img src='images/{$row->image}' alt='{$row->alttext}'>
</div>
{$content}
</p>
";
}
}
Or break the string:
if ($results->num_rows) {
while ($row = $results->fetch_object()) {
echo "<hr>
<h3>". htmlentities($row->title) ."</h3>
<p>{$row->created}</p>
<p>
<div id='blog-image'>
<img src='images/{$row->image}' alt='{$row->alttext}'>
</div>
". nl2br(htmlentities($row->content)) ."
</p>
";
}
}
The neatest way is to abstract the fetch process, so that the fact that data needs demangling, is abstracted away from the view process which draws the result.
Add a function :-
function demangle_row($row) {
$row->content = nl2br($row->content);
$row->content = htmlentities($row->content);
$row->title = htmlentities($row->title);
}
Now change the while statement, to use the above code so
While($row = $results->fetch_object()) {
becomes
While($row = demangle_row($results->fetch_object())) {
i'm coding a project and have some troubles when the system inform error:
Invalid argument supplied for foreach() in:
foreach($dbh->query($q1) as $row)
and can't get data from the database. How can i fix it, im a newbie, so if i don't understand, please teach me! thanks!
thank for your helps but i still can't fix it
<?php include("top.html"); ?>
<body>
<div id="main">
<h1>Results for <?php echo $_GET['firstname'] . " " . $_GET['lastname'] ?></h1> <br/><br/>
<div id="text">All Films</div><br/>
<table border="1">
<tr>
<td class="index">#</td>
<td class="title">Title</td>
<td class="year">Year</td>
</tr>
<?php
$dbh = new PDO('mysql:host=localhost;dbname=imdb_small', 'root', '');
$q1 = "SELECT id
FROM actors
WHERE first_name = '".$_GET['firstname']."' AND last_name = '".$_GET['lastname']."'
AND film_count >= all(SELECT film_count
FROM actors
WHERE (first_name LIKE'".$_GET['firstname']." %' OR first_name = '".$_GET['firstname']."')
AND last_name = '".$_GET['lastname']."')";
$id = null;
foreach($dbh->query($q1) as $row){
$id = $row['id'] ;
}
if($id == null){
echo "Actor ".$_GET['firstname']." ".$_GET['lastname']."not found.";
}
`
$sql2 = "SELECT m.name, m.year
FROM movies m
JOIN roles r ON r.movie_id = m.id
JOIN actors a ON r.actor_id = a.id
WHERE (r.actor_id='".$id."')
ORDER BY m.year DESC, m.name ASC";
$i = 0;
foreach($dbh->query($sql2) as $row){
echo "<tr><td class=\"index\">";
echo $i+1;
echo "</td><td class=\"title\">";
echo $row['name'];
echo "</td><td class=\"year\">";
echo $row['year'];
echo "</td></tr>";
$i++;
}
$dbh = null;
?>
</table>
</div>
</div>
<?php include("bottom.html"); ?>
</body>
</html>
Check that your query succeeded before iterating on the result:
if (false !== ($result = $dbh->query($d1))) {
foreach($result as $row){
$id = $row['id'] ;
}
}
By the way, I don't understand what you are trying to do with this pointless loop.
You could do this
$st = $dbh->query($q1);
if ( $st ) {
while ( $row = $st->fetch() ) {}
}
Try to make your code more readable
$result = $dbh->query($q1);
foreach($result as $row){$id = $row['id'];}
Where should I place a the image enable to appear, and what code should be insert I mean how to put some ('admin/savephp/images/<?php echo $rows['image']; ?>') because this is the place where the image was place
<?php
include('../connect.php');
$result = $db->prepare("SELECT * FROM candposition ORDER BY posid ASC");
$result->bindParam(':userid', $res);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$dsds=$row['posid'];
$resulta = $db->prepare("SELECT sum(votes) FROM candidates WHERE posid= :a");
$resulta->bindParam(':a', $dsds);
$resulta->execute();
for($i=0; $rowa = $resulta->fetch(); $i++){
$dsada=$rowa['sum(votes)'];
}
echo '<div style="margin-top: 18px;">';
echo '<strong>'.$row['pos_name'].' '.'</strong><br>';
$results = $db->prepare("SELECT * FROM candidates,users WHERE candidates.idno=users.idno AND posid= :a ORDER BY votes DESC");
$results->bindParam(':a', $dsds);
$results->execute();
for($i=0; $rows = $results->fetch(); $i++){
if($dsds=='Commissoner'){
echo $rows['course'].' '.$rows['name'].' = '.$rows['votes'];
} else {
echo $rows['lastname'].' = '.$rows['votes'];
}
$sdsd=$dsada
?>
<img src="../img/percent.gif"width='<?php echo(100*round($rows['votes']/($sdsd),2)); ?>'height='10'>
<?php
echo(100*round($rows['votes']/($sdsd),2));
?>
%<br>
<?php
}
}
?>
You can do this whereever you need the image
echo '<img src="admin/savephp/images/'.$rows['image'].'" \>';
From table, i am fetching $rows['colorVariants']; but its value i am getting like:-
[JEADV9Q23ESG25HY,JEADV9Q2NFRNYV5Q,JEADV9Q2PNBTXGNX,JEADV9Q2XKWPXWSX,JEADY8ABWH9XNF4B,JEADZWEBDHWRJ2NQ,JEADZWEBRWZ4B4VS,JEADZWEBXDCGSYCF,JEAEYYX95YYC4DRA]
Then i used substr to remove square bracket.
Explode this value and again need to fetch it it the same table for similar products, but i am not getting any result.
<?php
$table = table1; // Table Name
$mysqli = mysqli connection // Mysqli Connection here
$result = $mysqli->query( "SELECT * FROM $table_name WHERE `id` = '$q' ");
while ( $rows = $result->fetch_assoc() ) {
$rows['colorVariants'] = [JEADV9Q23ESG25HY,JEADV9Q2NFRNYV5Q,JEADV9Q2PNBTXGNX,JEADV9Q2XKWPXWSX,JEADY8ABWH9XNF4B,JEADZWEBDHWRJ2NQ,JEADZWEBRWZ4B4VS,JEADZWEBXDCGSYCF,JEAEYYX95YYC4DRA] // Result Copy Sample
// substr using for remove square bracket
$colorVariants = substr($rows['colorVariants'], 1);
$newColorVariants = substr($colorVariants, 0, '-1');
$finalColorVariants = explode(',', $newColorVariants);
}
?>
<?php
for($i = 0; $i < count($finalColorVariants); $i++) {
$color = $finalColorVariants[$i];
$color = $mysqli->real_escape_string($color);
$colorVariants = $mysqli->query( "SELECT id,store,title,imageUrlStr,mrp,price,productBrand FROM $table_name WHERE `productId` = '".$color."' ");
if ($colorVariants) {
while ($rows = $colorVariants->fetch_assoc()) {
$id1 = $rows['id'];
$store1 = $rows['store'];
$title1 = $rows['title'];
$imageUrlStr1 = $rows['imageUrlStr'];
$mrp1 = $rows['mrp'];
$price1 = $rows['price'];
$productBrand1 = $rows['productBrand'];
}
?>
<div class="cloth-inner-similar-box">
<div class="cloth-inner-similar-boxin">
<img src="<?php echo $imageUrlStr1; ?>" />
</div>
<div class="cloth-inner-similar-boxin-offer">
<div class="cloth-inner-similar-boxin-offer-in">
<p>30% OFF</p>
</div>
</div>
<div class="cloth-inner-similar-boxin-head">
<?php echo $title1; ?>
</div>
<div class="cloth-inner-similar-price border-solid-bottom border-solid-top">
Best Buy # <?php echo $price1; ?>/-<?php echo $productBrand1; ?>
</div>
</div>
<?php
} }
$mysqli->close();
?>