I'm trying to display database entries on a page in an unordered list. I'm echoing my rows in my li's, my sql connection seems fine... I don't understand where I went wrong... Does anyone have an idea?
<body class="projects">
<?php
$mysqli = new mysqli("localhost", "root", "root", "project");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT * FROM companies";
$result = $mysqli->query($query);
?>
<div id="projects" class="container">
<h1>Projects</h1>
<?php
while($row = $result->fetch_row());
?>
<ul>
<li>
<h2><?php echo $row['name']; ?></h2>
<p><?php echo $row['description']; ?></p>
</li>
</ul>
</div>
<?php
$result->close();
$mysqli->close();
?>
</body>
<ul>
<?php
while($row = $mysqli->fetch_row($result)) {
echo "<li>
<h2>".$row['name']."</h2>
<p>".$row['description']."</p>
</li>";
}
?>
</ul>
Or you can wrap the echo with {$row['item']} instead of what I did above with going in and out of HTML.
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();
}
<?php
$servername = "localhost";
$username = "khcy4dau_kesh";
$password = "";
$dbname = "khcy4dau";
//Create Connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check Connection
if ($conn->connect_error) {
die("Connection Failed : " . $conn->connect_error);
}
?>
</head>
<body>
<div class="container">
<div id="slideshow" class="slideshow">
<ul>
<li>
<div class="slide">
//How do I loop the data from the database so that it prints all instead of one by one//
<img class="icon" src="img/icons/heart.svg" alt="Heart Icon"/>
<blockquote>
<?
$sql = "SELECT quotations from quote";
if ($result = mysqli_query($conn,$sql))
{
$row = mysqli_fetch_row($result);
echo " $row['quotations'] ";
mysqli_free_result($result);
}
?>
</blockquote>
<?
echo "<p> $author </p>";
?>
</div>
</li>
<li>
<div class="slide">
<img class="icon" src="img/icons/letter.svg" alt="Letter Icon"/>
<blockquote>
<p>If you don't know where you are going, any road will get you
there.</p>
</blockquote>
<p>Lewis Carroll</p>
</div>
</li>
</ul>
</div>
</div><!-- /container -->
</body>
</html>
<?
$conn->close();
?>
You are probably looking for this.. but it's not particularly clear ..
I've assumed Author is in your quote table, and I have no idea what determines what Icon you use, so I was creative.. Decide what to use in the allocation from the query..
<?php
$servername = "localhost";
$username = "khcy4dau_kesh";
$password = "";
$dbname = "khcy4dau";
//Create Connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check Connection
if ($conn->connect_error) {
die("Connection Failed : " . $conn->connect_error);
}
$uberQuotes= array();
$sql = "SELECT quotations, author from quote";
if ($result = mysqli_query($conn,$sql))
{
while ($row = mysqli_fetch_row($result))
{
$uberQuotes['author'][] = array('quote'=>$row['quotations'], 'icon' => 'heart');
}
mysqli_free_result($result);
}
$conn->close();
if ($uberQuotes)
{
$quotesHTML = '<ul>';
foreach ($uberQuotes as $author => $quotes)
{
foreach ($quotes as $quote)
{
$quotesHTML .= '
<li>
<div class="slide">
<img class="icon" src="img/icons/'.$quote['icon'].'.svg" alt="'.ucFirst($quote['icon']).' Icon"/>
<blockquote>
'.$quote['quote'].'
</blockquote>
<p> '.$author.' </p>
</div>
</li>';
}
}
$quotesHTML .= '</ul>';
}
else
{
$quotesHTML = 'No Quotes Found';
}
?>
</head>
<body>
<div class="container">
<div id="slideshow" class="slideshow">
<?=$quotesHTML?>
</div>
</div><!-- /container -->
</body>
</html>
You can use any looping statements, gave you example foreach loop, fetch data into $items and loop the array:
<?php foreach($items as $item): ?>
<li><div class="slide"></div></li>
<?php foreach; ?>
Try something like this
<ul>
<?php foreach($items as $item): ?>
<li><div class="slide"></div></li>
<?php foreach; ?>
</ul>
I used jquery-ui tabs using php but it's not working, I am unable to identify the problem, please help me.
<div id="tabs">
<div class="row">
<div class="col-md-4">
<?php
$con = mysqli_connect("localhost", "admin", "123456", "gazette");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM gazette_details");
while ($row = mysqli_fetch_array($result)) {
echo "<ul>";
$id = '#tabs-' . $row['id'];
echo "<li>" . "<a href='$id'>" . $row['gazette_id'] . "</a>" . "</li>";
echo "</ul>";
}
mysqli_close($con);
?>
</div>
<div class="col-md-4">
<div id="tabs-12">1</div>
<div id="tabs-13">2</div>
<div id="tabs-14">3</div>
</div>
</div>
<div class="col-md-4">.col-md-4</div>
</div>
I used this website example: http://jqueryui.com/tabs/
you may try
<?php
$ARR_RESULT = array();
$con = mysqli_connect("localhost", "admin", "123456", "gazette");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM gazette_details");
while ($row = mysqli_fetch_array($result)) {
$ARR_RESULT[] = $row;
}
mysqli_close($con);
?>
<div id="tabs">
<ul>
<?php
foreach($ARR_RESULT as $arr)
{
echo '<li>'.$arr['gazette_id'].'</li>';
}
?>
</ul>
<?php
foreach($ARR_RESULT as $arr)
{
echo '<div id="tabs-'.$arr['id'].'">';
echo '<p>Tab-'$arr['id'].'</p>';
echo '</div>';
}
?>
</div>
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 } ?>