I am trying to get the code below to loop using a while statement. The problem I am having is getting the divs to worth with in the PHP. Any ideas on how I can make this happen? My database consists of a columns called month, day, description, and location.
<div class="events-module">
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sandbox", $con);
$result = mysql_query("SELECT * FROM events"); ?>
<?php while($row = mysql_fetch_array($result)) ?>
<div class="date">
<div id="month"><h4><?php echo $row['month']; ?></h4></div>
<div id="day"><h3><?php echo $row['day']; ?></h3></div>
</div>
<div class="event">
<div id="description"><p><?php echo $row['description']; ?></p></div>
<div id="location"><p><b><?php echo $row['location']; ?></b></p></div>
</div>
<?php endwhile;
mysql_close($con);
?>
</div>
You didn't say exactly what's wrong. But, it seems you are missing your start block for your while loop
while (.... ) :
^^^
... // div stuff
end while
Related
I am able to get values displayed from database when using mysqli_multi_query.But I am not able to print in my HTML code.Its Probably the issue with while loop. Below is my code which gives me the AVG for the three SQL statements. Only problem is with displaying average value in HTML.
<?php
$con = new mysqli("localhost", "root","","survey");
if ($con){ printf(""); }
$query = "SELECT AVG(workarea) FROM score;" ;
$query .= "SELECT AVG(manager) FROM score;";
$query .= "SELECT AVG(comm) FROM score;";
// Execute multi query
if (mysqli_multi_query($con,$query)) {
do {
if ($result=mysqli_store_result($con)) {
// Fetch one and one row
while ($row=mysqli_fetch_row($result)) {
printf("%s\n",$row[0]);
}
// Free result set
mysqli_free_result($result);
}
} while (mysqli_next_result($con));
}
?>
<div class="row count">
WORK AREA/UNIT SCORE : <?php echo $row[0]; ?> // echo $row[AVG(workarea)] doesnot work
</div>
<div class="row count">
SUPERVISOR/MANAGER SCORE : <?php echo $row[0]; ?> // echo $row[AVG(manager)]
</div>
<div class="row count">
COMMUNICATION SCORE : <?php echo $row[0]; ?> // echo $row[AVG(comm)] doesnot work
</div>
No need of multi query you just get AVG in single query. And write your html inside while loop as
$con = new mysqli("localhost", "root", "", "survey");
if ($con) {
printf("");
}
$query = "SELECT AVG(workarea),AVG(manager),AVG(comm) FROM score;";
// Execute multi query
if ($result = $con->query($query)) {
/* fetch object array */
while ($row = $result->fetch_row()) {
?>
<div class="row count">
WORK AREA/UNIT SCORE : <?php echo $row[0]; ?>
</div>
<div class="row count">
SUPERVISOR/MANAGER SCORE : <?php echo $row[1]; ?>
</div>
<div class="row count">
COMMUNICATION SCORE : <?php echo $row[2]; ?>
</div>
<?php
}
/* free result set */
$result->close();
}
I am trying to repeat a div the same div multiple times with as many rows as i have in the database table. a non working example i made of what i want is below.
<?php for ($i=0; $i < 3; $i++) {
?><!-- switch the three with the number of rows in the table-->
I would want to loop the container and switch the database query with the value of i.
<div id = "bodyContainer">
<form class="review">
<div class="reviewContainer">
<div class = "images">
<?php
$link = mysqli_connect("localhost", "root", "root","DJP");
if (mysqli_connect_error()) {
die("Could not connect to database");
}
$query = "SELECT * FROM reviews WHERE id = '<?php echo $i ?><!--switch the value of i here-->'";
$result=mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
echo '<img height = 90% width = 100% src="data:image/jpeg;base64,'.$row[3].' " />';
?>
</div>
<!--<div class="title">
<?php
//echo $row[1];
?>
</div>-->
<div class = "comment">
<?php
echo $row[2];
?>
</div>
<div class = "rating" style = "float: right;">
<?php
echo $row[4];
?>
<br/>
<br/>
stars
</div>
</div><!--end review-->
</form>
</div><!--end body container-->
<?php;}?><!-- end the for loop so all the divs can be re-done with a new entry for the query. I would move the $link out of loop also.-->
Just fetch all the rows once and then go through them as you print the data:
$link = mysqli_connect("localhost", "root", "root","DJP");
if (mysqli_connect_error()) {
die("Could not connect to database");
}
$query = "SELECT * FROM reviews";
$result=mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)){ ?>
<div id = "bodyContainer">
<form class="review">
<div class="reviewContainer">
<div class = "images">
<?php
echo '<img height = 90% width = 100% src="data:image/jpeg;base64,'.$row[3].' " />';
?>
</div>
<!--<div class="title">
<?php
//echo $row[1];
?>
</div>-->
<div class = "comment">
<?php
echo $row[2];
?>
</div>
<div class = "rating" style = "float: right;">
<?php
echo $row[4];
?>
<br/>
<br/>
stars
</div>
</div><!--end review-->
</form>
</div><!--end body container-->
<?php } ?>
My php slider is sort of working i have managed to get it to link to my database. However, i need it to loop through all the images within my slider and my code isn't work but i believe i have missed a loop query?!
THIS IS MY CODE WHERE MY SLIDER IS:
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
<img src="<?php print $row['image']?>"/>
<!-- <img src="images/nivo/arts.png" alt="the grand theatre and nothern ballet">
<img src="images/nivo/slider3.png" alt="leeds night light slider image">
<img src="images/nivo/slider2.png" alt="Leeds Trinity slider image">
<img src="images/nivo/slider4.png" alt="leeds art hotels">
<img src="images/nivo/slider1.png" alt="leeds art slider image"> -->
</div>
</div>
THIS IS MY CODE TO RUN MY SLIDER AT THE MOMENT:
<?php
$myQuery = "SELECT * FROM SliderImg";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));
$row = mysqli_fetch_array($result);
?>
you need a loop in your php:
<?php
$myQuery = "SELECT * FROM SliderImg";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));
$rows = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$rows[] = array(
'image' => $row['image'];
)
}
?>
And in your HTML:
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
<?php
foreach($rows as $row) { ?>
<img src="<?php print $row['image']?>"/>
<?php
} ?>
</div>
</div>
<?php
$myQuery = "SELECT * FROM SliderImg";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));
?>
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
// use a while here
<?php while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){ ?>
<img src="<?php print $row['image']?>"/>
<?php } ?>
</div>
</div>
you can use foreach to loop through your data, like this
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
<?php foreach ($row as $key => $item): ?>
<img src="<?php echo $item['image']?>"/>
<?php endforeach; ?>
</div>
</div>
hope it help..
I am building a blog and trying to show all comments that apply to the post.
Each post has an ID and each comment is stored with the post ID.
here is my code:
<?php
$con = mysql_connect("localhost","cl49-XXX-b","X");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cl49-XXX-b", $con)or die( "Unable to select database line 873");
$result=mysql_query("SELECT * FROM blogcomments WHERE ID='".$ID."'") or die('Error on Line 215 :'.mysql_error());
echo " <ul class='comments'> "; // first row beginning
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$name = $row['name'];
$email = $row['email'];
$comment = $row['comment'];
$created=$row['created'];
echo" <li>
<div class='comment'>
<div class='thumbnail'>
<img class='avatar' alt='' src='img/avatar.jpg'>
</div>
<div class='comment-block'>
<div class='comment-arrow'></div>
<span class='comment-by'>
<strong>$name</strong>
<span class='pull-right'>
<span> <a href='#'><i class='icon-reply'></i> Reply</a></span>
</span>
</span>
<p>$comment</p>
<span class='date pull-right'>$created</span>
</div>
</div> ";
echo " </li>"; // it's time no move to next row
}
?>
When I run this code the page only shows one comment, although my DB has 3 comments with the correct ID.
I would consider using mysqli_ as mysql_ has been depreciated. I'd also consider using a while loop, hopefully this will help:
<?php
$DBServer = 'localhost';
$DBUser = 'xxxx';
$DBPass = 'xxxx';
$DBName = 'xxxx';
$mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if ($mysqli->connect_errno) {
echo "Failed to connect to the database: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$query = "SELECT * FROM blogcomments WHERE ID='". $ID ."'";
echo " <ul class='comments'> ";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
?>
<li>
<div class='comment'>
<div class='thumbnail'>
<img class='avatar' alt='' src='img/avatar.jpg'>
</div>
<div class='comment-block'>
<div class='comment-arrow'></div>
<span class='comment-by'>
<strong><?php echo $row['name']; ?></strong>
<span class='pull-right'>
<span><a href='#'><i class='icon-reply'></i> Reply</a></span>
</span>
</span>
<p><?php echo $row['comment']; ?></p>
<span class='date pull-right'><?php echo $row['created']; ?></span>
</div>
</div>
</div>
</li>
<?php
} $result->close();
} $mysqli ->close();
echo "</ul>";
?>
I haven't tested this for bugs, but let me know if you like further information.
When you do this :
mysql_query("SELECT * FROM blogcomments WHERE ID='".$ID."'")
You select just one comment in the database.
Maybe you have to do that :
mysql_query("SELECT * FROM blogcomments WHERE post_ID='".$post_ID."'")
Because you are selecting the row where ID field value is $ID in the table blogcomments.
I guess you store the referred post_id to whom the comment is connected in a field called something like "post_id". You better point your select query to that field ;)
If you put the structure of the table in the question, I might help :)
You're not closing your <ul>. Might it just be a HTML formatting issue?
i tried the empty selector in jquery but it doesnt work. the content is still being displayed. i am retrieving some rows from the SQL database.. if there is no database then i dont want to display that div.
<div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
</div>
Is that your whole code? Are you working with ajax? If you're using pure PHP without ajax just set the output div into your "if"-conditions?
If you want to use jQuery, try:
if ( ($("div.scrollableArea p").text()).length > 0 )
{
$("div.scrollableArea p").show();
}
else
{
$("div.scrollableArea p").hide();
}
But you can do it without jQuery, I guess.
You need to put the HTML inside your php if clause.
Simple example:
<div id="scrollingText">
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysqli_connect_errno($con)) {
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
<?php } else { ?>
<div class="errordiv">Display this on error</div>
<?php } ?>
</div>
thank you guys with ur help i made some change. the below code did it:
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysql_num_rows($result) > 0)
{
?><div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?> </p>
</marquee>
</div>
</div>
</div>
<?php } ?>