<?php
$getNews = $db->prepare("SELECT * FROM news ORDER BY id DESC LIMIT 4");
$getNews->execute();
$news = $getNews->fetchAll();
foreach ($news as $newspost) {
echo $newspost['title'] ; ?> <a style="cursor:pointer;" onclick="return toggleMe('problem')">read/hide</a>
<?php
echo '<br />';
echo 'Posted by '; echo $newspost["user"]; echo ' at '; echo $newspost["created"]; ?>
<div id="<?php echo $newspost['id']; ?>" style="display:none;">
<?php
echo $newspost['message'];
?>
</div>
<?php
echo '<br /> <br />';
}
?>
What I had in mind was that it shows/hides the text from the newspost when you hit the read/hide link next to the title of the newspost.
I can fit the $newspost['id'] in the representing div but because the onclick="return toggleMe('problem')"> has both " and ' in it I need another way to fit it in there, I searched a lot but couldn't find what I was exactly looking for.
Actually, you'll be fine with:
"return toggleMe('<?php echo $newspost['id']; ?>');"
The inner single quotes will never be seen in the output HTML, and the <?php ?> block doesn't care what quotes surround it.
Related
When I run the following file I get the database data i.e it prints it out on the website so I know my connections are good.
<html>
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo $row["name"], $row["image"];
}
?>
</div>
</html>
However when I try and format the results like below
<html>
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo <div id = "bookbar">
<img src= "$row['image']" alt = "image">
<p> $row['name'] </p>
</div>
}
?>
</div>
</html>
it doesn't work. Can anyone help me fix the code?
Maybe try this your code didn't close/open the php tags properly also don't echo like that
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<div id = "bookbar">
<img src= "<?php echo $row['image'] ?>" alt = "image">
<p><?php echo $row['name']; ?></p>
</div>
<?php
}
}
$conn->close();
?>
If you want to echo something out
Its better to close on open the php tags like so
PHP goes here
?>
HTML goes here
<?php
PHP goes here
And if you want to echo something inside the HTML just do this
<span> <?php echo "something" ?> </span>
much easier and makes the code easier to read.
change your echo statement to -
echo '<div id = "bookbar"><img src= "' . $row['image'] . '" alt = "image"><p>'. $row['name'] .'</p>'
Your issue is a syntax problem - you can't use echo like that, it has to echo a string variable. You should be seeing an error message about it.
You could keep the echo statement and put all the HTML inside a string, and concatenate (or interpolate) the PHP data into it. But IMO the easiest thing here in terms of readability and maintenance is to step out of the PHP tags, print the HTML, embed some PHP tags in it for the variables, and then step back in again to continue with the code. It makes the HTML far easier to understand:
?>
<div id="bookbar">
<img src="<?php echo $row['image'] ?>" alt="image">
<p><?php echo $row['name'] ?></p>
</div>
<?php
When you are in php mode you should echo strings as php variables wrapped with single quotes:
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo '<div id = "bookbar">';
echo '<img src="' . $row['image'] . '" alt = "image">';
echo '<p>' . $row['name'] . '</p>';
echo '</div>';
}
From the image below, you can see there is a empty space in second row (red rectangular), by right it should filling up the empty space. This is happening because the first image title is longer than others.
I know by using HTML code, I can use clear: both; and put under the first row to solve it. But my info is read from database. Following is my php and mysql code, I have simplified it, please ignore the method I use to display the image.
$selectSQL="SELECT *, DATE_FORMAT(published_date,'%d %M %Y') AS eventDate FROM media";
while($rows=mysql_fetch_array($selectSQL)){
<div style="float:left">
<img src="load_image.php?id=<?php echo $rows["id"]; ?>" width="150px" height="212px">
<div><?php echo $rows["title"]; ?></div>]
<div><?php echo $rows["eventDate"]; ?></div>
</div>
}
I have one solution, which is compare the number of element = 5. Something like the following code. But I'm not sure how to check it the number of element. Please advice or please suggest me if you have another solutions. Thanks!
if($num_element==5){
echo '<div class="clear"></div>';
}
$ i = 0;
while($rows=mysql_fetch_array($selectSQL)){
<div style="float:left">
<img src="load_image.php?id=<?php echo $rows["id"]; ?>" width="150px" height="212px">
<div><?php echo $rows["title"]; ?></div>]
<div><?php echo $rows["eventDate"]; ?></div>
</div>
$i++;
if($i % 5 == 0){
echo '<div class="clear"></div>';
}
}
you can try this code to check the number of items totally displayed and after 5 elements it will print a clear div
You can use substr() to concat your title when it is very long. you can try it like this:
while($rows=mysql_fetch_array($selectSQL)){
<div style="float:left">
<img src="load_image.php?id=<?php echo $rows["id"]; ?>" width="150px" height="212px">
<div title="<?php echo $rows["title"];?>">
<?php if(strlen($rows["title"]) > 6){
echo substr($rows["title"],0, 6).'...';
}else{
echo $rows["title"];
} ?>
</div>
<div><?php echo $rows["eventDate"]; ?></div>
</div>
}
With this code you can customize the length of the words in your title.
I have been trying to put HTML inside PHP for a while, but I could never get it to work. Instead I have had to use this
<?php //code in here
while($record = mysql_fetch_array($myData)){
?>
//HTML code
<?php
}
?>
and it has worked for a while, but now I'm implementing a search function to the page, and I need all HTML to be dynamically generated, or the page will be displayed twice. Therefore, i'm in need of some help. This is the old-new code I have at the moment.
<?php
$sql = "SELECT * FROM `LISTINGS` ORDER BY YA DESC $limit";
$myData = mysql_query($sql,$con);
while($record = mysql_fetch_array($myData)){
//this is what i have manage to get done
echo '<div class="cards-row">';
echo '<div class="card-row">';
echo '<div class="card-row-inner">';
echo '<div class="card-row-image" data-background-image="http://domains/$record["IMAGENAME"].jpg">';
echo '</div>';
echo '<div class="card-row-body">';
echo '<h2 class="card-row-title">';
echo '<a href="http://domain/listing-detail.php?ID='; $record['ID']echo'">'
echo '</a>';
echo '</h2>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
?>
Static/dynamic code
<div class="card-row">
<div class="card-row-inner">
<div class="card-row-image" data-background-image="http://www.domain/<?php echo $record['IMAGENAME'];?>.jpg" width="150" height="180" ">
</div><!-- /.card-row-image -->
<div class="card-row-body">
<h2 class="card-row-title">
<a href="http://www.domain/listing-detail.php?ID=<?php echo $record['ID'];?>"> <?php custom_echo ($record['TITLE'], 80); ?><?php if($record['STREET']===' '){ echo "Not provided" ;}?>
</a>
</h2>
<div class="card-row-content"><?php custom_echo ($record['DESCRIPTION'], 250); ?><?php if($record['STREET']===' '){ echo "Not provided" ;}?></div><!-- /.card-row-content -->
</div><!-- /.card-row-body -->
<div class="card-row-properties">
<dl>
<dd></dd><dt>Visit Website</dt>
<dd></dd><dt>More Info</dt>
<dd>Added</dd><dt><?php echo $record['YA'];?>/<?php echo $record['MU'];?>/<?php echo $record['DU'];?></dt>
<dd>Viewed</dd><dt>Visited</dt>
</dl>
</div><!-- /.card-row-properties -->
</div><!-- /.card-row-inner -->
</div><!-- /.card-row -->
</div><!-- /.cards-row -->
<br/>
<?php
}
?>
My goal is to put all the code above inside
while($record = mysql_fetch_array($myData)){
Although this is not the best way when it comes to readability and maintainability, the way I usually process HTML with PHP is by echoing it.
echo "<div style=\"color:red\">test</div>";
If I need to add PHP content then
$myVar = "test";
echo "<div style=\"color:red\">" . $myVar . "</div>";
And if I already have some HTML code I'd like to escape, I can easily do so using Notepad++'s replace all function by replacing " with \"
You have an error in line 15
echo '<a href="http://domain/listing-detail.php?ID='; $record['ID']echo'">'
Change it to:
echo '<a href="http://domain/listing-detail.php?ID='.$record['ID'].'">';
Also, are you saving it in ".php" extension?.
After the last echo, you forget a '}'.
Finally, remove your mysql_ function and use mysqli_ or PDO, if you're on PHP7.
I have a question that bothers me for a long time. Lets say i have a code with several nested div's and span's. All these compose a square with an image inside.
echo '<div> <div> <div> <div> <span> <img src='.$image.'> </span></div></div></div>';
Only that this code has about 15 rows.
From what i know when echo-ing the results from db in that form i put in the loop the whole html code. It looks clumsy this way.
It is there a better practice ?
foreach ($query->result() as $row)
{
$row->address_link=strtolower($row->network);
echo '<li class="col-md-3 isotope-item '.$row->network.'">';
echo '<div class="portfolio-item img-thumbnail">';
echo '<table border="0"><tr>';
echo '<a href="order/'.$row->address_link.'/'.$row->value.'" class="thumb-info">';
echo '<img alt="" class="img-responsive" src="img/'.$row->address_link.'.png">';
echo '<span class="thumb-info-title">';
echo '<span class="thumb-info-inner">'.$row->value.' Euro</span>';
echo '</span>';
echo '<span class="thumb-info-action">';
echo '<span title="Universal" href="order/'.$row->address_link.'/'.$row->value.'" class="thumb-info-action-icon"><i class="icon icon-link"></i></span>';
echo '</span>';
echo '</a>';
echo '</div>';
echo '</tr><tr>';
echo '<span class="thumb-info-type">'.$row->value*$row->rate.' Eur</span>';
echo '</tr></table>';
echo '</li>';
}
If you are new to php you can define a function for this:
function wrapImage($src){
return '<div> <div> <div> <div> <span> <img src='.$src.'> </span></div></div></div>';
}
And just use echo wrapImage($src) where you need it with different params.
EDIT: consider following way of presenting the data:
<?php
$query = 'select * from Unicorns';
foreach ($query->result() as $row){
$row->address_link=strtolower($row->network);
?>
<!-- html -->
<li class="col-md-3 isotope-item <?php echo $row->network; ?>">
<div class="portfolio-item img-thumbnail">
<table border="0"><tr>
<a href="order/<?php echo $row->address_link.'/'.$row->value; ?>" class="thumb-info">
<img alt="" class="img-responsive" src="img/'.$row->address_link.'.png">
<span class="thumb-info-title">
<span class="thumb-info-inner"><?php echo $row->value; ?> Euro</span>
</span>
<span class="thumb-info-action">
<span title="Universal" href="order/<?php echo $row->address_link.'/'.$row->value ?>" class="thumb-info-action-icon"><i class="icon icon-link"></i></span>
</span>
</a>
</div>
</tr><tr>
<span class="thumb-info-type"><?php echo ($row->value*$row->rate); ?> Eur</span>
</tr></table>
</li>
<!-- /html -->
<?php } ?>
It is called spaghetti code .. and it is NOT the best practice but is better then your example in case the HTML is more then the PHP data.
first of all, dont use echo in loops (optimalization), store your output in variable and print it only once.
Repeated code can be stored inside function
function square($image){
return '<div> <div> <div> <div> <span> <img src='.$image.'> </span></div></div></div>';
}
$output = '';
while ($loop){
$output .= square($image);
}
echo $output
I want to retrieve data from database using ajax the data is retrieving successfully but it is not showing properly on page. Maybe it is the issue of if else condition.
Here is my code:
<?php
$query = mysql_query("SELECT * FROM cart_polling WHERE country = '".$_REQUEST['id']."' ORDER BY sort_order ASC") or die(mysql_error());
$record = mysql_num_rows($query);
while($getcontry = mysql_fetch_array($query)){
?>
<div class="Question_Table">
<div class="Question_Title">Q<?php echo $getcontry['sort_order']; ?>-<?php echo $getcontry['question']; ?></div>
<!-- ****** Box1 START ****** -->
<?php
if($getcontry['img1']!=""){
?>
<div class="bg">
<?php
if(isset($_SESSION['id'])){
?>
<div class="img"><center>
<a href="javascript:void(0)" class="op">
<img src="images/pollimg/<?php echo $getcontry['img1']; ?>" width="104" height="102" class="middleimg2" onclick="getValue('<?php echo $_REQUEST['id']; ?>','<?php echo $getcontry['option1']; ?>','<?php echo $getcontry['id']; ?>','1','<?php echo $getcontry['img1'] ?>','<?php echo $getcontry['categoryname'] ?>','<?php echo $getcontry['question']; ?>')" /></a>
</center></div>
<?php } ?>
<div class="Vote_button">
<img src="images/vote_button.png" onclick="sendvar();" align="middle">
<img src="images/view_results.png" onClick="showresult(<?php echo $getcontry['id']; ?>)" align="middle">
</div>
<?php
}
</div>
<?php } ?> // here the while loops end.
I want (<div id="txtHint<?php echo $getcontry['id']; ?>"></div>) this in else condition
and the above code in if condition when I click on image viewresult.png a function call which retrieve data from database. I want the data will show in else condition and the above code including loop shows in if condition.
The txtHint($getcontray['id']) is the id of every record whose value changes dynamically with the help of while loop. And txthint is also saves the response of ajax.
If I am using if condition than while loop is not running in else condition and the id can't pass to AJAX response and the correct data will not show, so how can I do that?
in line: <img src="images/pollimg/
'<?php echo $getcontry['img1'] ?>','<?php echo $getcontry['categoryname'] ?>'
missing 2 semicolon.
'<?php echo $getcontry['img1']; ?>','<?php echo $getcontry['categoryname']; ?>'
UPDATE:
And missing ?> php end syntax at the bottom of your file:
<?php
}
//here is the missing tag:
?>
</div>
<?php } ?> // here the while loops end.