I have a table in MySQL and I want to check in PHP whether there are any 'tasks' written (it's a todo list webapp). If there aren't I want to display a message 'You haven't added any tasks yet', problem is it's not displaying.
PHP is assuming that the list of tasks is never empty, when for user 1 (in PHP current user_id = 2), for instance, it is, since I haven't inserted any tasks in my MySQL for User 1 (all tasks are user 2).
<?php
global $connection;
$user = $_SESSION['user_id'];
$query = "SELECT * FROM to_do_list WHERE user = $user";
$items = mysqli_query($connection, $query);
?>
<div>
<?php if(!empty($items)): ?>
<ul id="myUL">
<?php foreach($items as $item): ?>
<li>
<span class="item<?php echo $item['done'] ? ' done' : '' ?>"><?php echo $item['task']; ?></span>
<?php if(!$item['done']): ?>
Done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>You haven't added any tasks yet.</p>
<?php endif; ?>
</div>
mysqli_query returns a "resource" not an array - so:
instead of foreach($items as $item): use while($item = mysqli_fetch_assoc($items))
and now every $item is an associated array of results (meaning - you can call the output using it's name like $item['done'])
also - instead of if(!empty($items)) you should use if(mysqli_num_rows($items) > 0)
Related
<?php include ("../navbar.php"); ?>
<?php include("../conn.php");
$query = "select * from semi_synthetic";
$result = mysqli_query($conn,$query);
?>
<div class="container">
<div class="compound_report_card">
<h2>Compound Report</h2>
<div class="card-content">
<?php
//Check If Id Isset.
if(isset($_GET["CHIHBT_ID"]))
//Display Id On A Webpage.
echo "<p>Calculated Properties of : " . $_GET[ "CHIHBT_ID" ] . "</p>";
$result = mysqli_query($conn, $query);
?>
<?php
while($row = mysqli_fetch_assoc($result )) {
?>
<ul>
<li>Canonical smiles : <?php echo $row['smile_notation']; ?> </li>
<li> Molecular weight : <?php echo $row['molecular_weight']; ?></li>
<li> Heavy atoms : <?php echo $row['number_of_heavy_atoms']; ?></li>
<li> Aeromatic heavy atoms : <?php echo $row['number_of_aeromatic_heavy_atoms']; ?></li>
<li> Rotatable bonds : <?php echo $row['number_of_rotatable_bonds']; ?></li>
<li> H bond acceptors : <?php echo $row['number_of_h_bond_acceptors']; ?></li>
<li> H bond donors: <?php echo $row['number_of_h_bond_donors']; ?></li>
<li> Tpsa A**2 : <?php echo $row['tpsa']; ?></li>
</ul>
<?php } ?>
</div>
</div>
</div>
<?php include ("../footer.php"); ?>
so this is my new code and i am displaying data on web from my database and . So what i am trying to do is i have a lot of id's and i want that when i click on any id the data of that id from
database should get displayed. So the current situation is that when i am clicking on one id whole data from that table is getting displayed and i want only one to get displayed. I want onlyspecific data from every id that i click. So the problem i think is in while loop so please see that also
The problem: Your code is getting all records and isn't querying against your selected ID.
What you need to do: Add selected ID in query to get specific result. Like shown below:
Update: From What I understand $_GET["CHIHBT_ID"] is your ID, right? Change this code in your script:
<?php include("../conn.php");
$query = "select * from semi_synthetic";
$result = mysqli_query($conn, $query);
?>
To
<?php include("../conn.php");
$query = "select * from semi_synthetic";
?>
Then, change the following code:
<?php
//Check If Id Isset.
if (isset($_GET["CHIHBT_ID"]))
//Display Id On A Webpage.
echo "<p>Calculated Properties of : " . $_GET["CHIHBT_ID"] . "</p>";
$result = mysqli_query($conn, $query);
?>
With this:
<?php
//Check If Id Isset.
if (isset($_GET["CHIHBT_ID"])) {
$query .= ' WHERE CHIHBT_ID = ' . $_GET["CHIHBT_ID"]; // add selected id in query
//Display Id On A Webpage.
echo "<p>Calculated Properties of : " . $_GET["CHIHBT_ID"] . "</p>";
}
$result = mysqli_query($conn, $query); // run query
?>
and that's it.
I'm new to PHP and MySQL and right now I'm showing a table with categories from products. But I'm trying to only show them if they exist in another table. Unfortunately, I'm not sure how to get there.
Currently, I'm showing ALL categories like this:
<?php include '/../connect.php'; ?>
<?php
$results=$conn->query("SELECT * FROM groepen");
while($rows=$results->fetch_array())
{
?>
<li><?php echo $rows['GroepOmschrijving']; ?>
<?php
$result=$conn->query("SELECT * FROM subgroepen WHERE GroepID=".$rows['GroepID']);
?>
<ul>
<?php
while($row=$result->fetch_array())
{
?><li><?php echo $row['SubGroepOmschrijving']; ?></li><?php
}
?>
</ul>
</li>
<?php
}
?>
So, I'm trying to only show the categories if the value of table groepen.GroepID matches the value of occasions.Groepnummer.
I have an if statement
<p>Included Services</p>
<?php foreach($services as $service): ?>
<?php if(!empty($service['service_webdesign'])){ ?>
<div><strong>web Design:</strong> <?php echo $service['service_webdesign'] ?></div>
<?php } ?>
<?php endforeach ?>
<p>Services charged by the hour</p>
<?php foreach($services as $service): ?>
<?php if(!empty($service['service_webdesign'])){ ?>
<div><strong>Service:</strong> <?php echo $service['service_webdesign'] ?></div>
<?php } ?>
<?php endforeach ?>
There are three possible values Yes, hourly rate or no.
In the first block I want to be able to echo the service column values
<div><strong>Service:</strong> <?php echo $service['service_webdesign'] ?></div>
only if the database value is 'yes'.
In the second block I want to echo the service fee column
<div><strong>Web Design:</strong> <?php echo $service['service_fee_webdesign'] ?>
</div>
only if the value of the 'service_webdesign' is set to 'hourly rate' but I do not actually want to show the value of 'service_webdesign' ('hourly rate').
An example of my database table structure is as follows
service_webdesign = Yes
service_fee_webdesign = NULL
service_graphicdesign = Hourly
service_fee_graphicdesign = 20
How can I achieve that within an if statement? Or would I need to create another SQL query for each of the blocks?
For example: I have a page that lists book titles stored in a database. What I want to happen is WHEN it gets to a certain book title say something different or add additional info like "ON SALE".
Is there a function that exists that allows me to manipulate data inside a loop?
I've tried a couple of things, but have not gotten the desired results.
Here's my thought process:
Query:
$sql = "SELECT *
FROM `artist`
ORDER BY artist_name ASC";
$q = $db->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
Output:
<?php while ($r = $q->fetch()); ?>
<li class="<?php echo $r['sort_status']; ?>">
<figure>
<div class="gallery-img"><img src="books/<?php echo $r['photo']; ?>" alt="picture of <?php echo $r['book_title']; ?>"></div>
<figcaption>
<h3 class="padding-fifteen-bottom"><?php $book = $r['book_title']; if($book=="A Tale of Two Cities"){echo "A Tale of Two Cities - On Sale NOW! ";} else { echo htmlspecialchars_decode(truncate($book,34));}?></h3>
</figcaption>
</figure>
</li>
<!-- end work item -->
<?php endwhile; ?>
Yes!
if($book_title == "My Title")
{
// Logic here.
}
HTML format
<?php if($book_title == "My Title"): ?>
<p>my html</p>
<?php endif;?>
I store my testimonials in a database table and would like them to be displayed on my website via this for loop:
<?php
$count = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) cnt FROM testimonials"));
for ($i = 1; $i <= intval($count['cnt']); $i++)
{
$sql = mysql_query("SELECT * FROM testimonials WHERE id='{$i}'");
?>
<li class="span4">
<div class="thumbnail thumbnail-1">
<section>
<a class="link-1" style="cursor:pointer;"><?php echo $sql['name'] ?></a>
<p><?php echo $sql['text'] ?></p>
<?php echo $sql['product'] ?>
</section>
</div>
</li>
<?php
}
?>
The issue is that the $sql variables product, name and text are not displaying. However the $count is getting the correct intval, so it knows there are entries.
It's also worth pointing out that the loop is working as I get the <li> <div> and <section> tags working, the only issue is the <a>'s and the <p> not getting the textual value from the echo
P.S. I know that mysql_* functions are depreciated however my php version 5.3 and they are only depreciated from 5.5 so they are ok for my website.
you missed to fetch second sql
add this line
$result= mysql_fetch_assoc($sql) ;
and then call your variables like that
<?php echo $result['name'] ; ?>
<?php echo $result['product'] ;?>
<?php echo $result['product'] ; ?>
^^-----dont forget `;` because you missed them also