How can I echo inside an html tag? I read some articles here and I understood that I have to put the all HTML tag inside the PHP, but when I do, it won't show the correct layout. I mean, it puts it in the top of the page instead of showing it where it belong. How can I make this work? Attached two pictures to explain my broken english better
<?php
include("config.php");
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "SELECT id FROM users";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<div class='inner'> <h2 class='major'>How many accounts</h2> <p>We currently have ". $row["id"] . " accounts!</p></div>";
}
} else {
echo "0 results";
}
$con->close();
?>
And I would like to put only row id inside HTML tag because, in the HTML inside PHP it shows the first on the page, it doesn't show where it should belong.
<section id="four" class="wrapper alt style1">
<div class="inner">
<h2 class="major">How many accounts</h2>
<p>We currently have <?php echo $row["id"] ?> accounts!</p>
How it shows now:
How it should look
Why don't you echo $result->num_rows instead of id? What happen if someone close their account or you delete some rows in the middle of table?
I have reached to an answer.
in HTML section I added <?php echo $ID ?> and in the PHP section I modified it with
while($row = $result->fetch_assoc()) {
$ID = $row['id'];
}
Now script looks like this
<?php
include("config.php");
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "SELECT id FROM users";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$ID = $row['id'];
}
} else {
echo "0 results";
}
$con->close();
?>
and the HTML part
<h2 class="major">How many Accounts?</h2>
<p>Currently we have <?php echo $ID ?> accounts.</p>
Assign a variable to your result.
while($row = $result->fetch_assoc()) {
$id = $row["id"]; }
Then anywhere in your html all you will have to do is the following.
<?php echo $id; ?>
I hope this helps. Have a great day.
Related
I am relatively new to PHP and want to know a more efficient way of echo'ing the following in less steps or even just one statement.
$sql = "SELECT * FROM comments";
$result = OpenCon()->query($sql);
while( $row = $result->fetch_assoc()) {
echo "<div class='card'><p class: card-text>";
echo $row['uid']."<br>";
echo $row['date']."<br>";
echo $row['message']."<br><br>";
echo "</p></div>";
As #droopsnoot has commented on above, you should try doing the next thing here.
while( $row = $result->fetch_assoc()) {
echo "<div class='card'><p class: card-text>". $row['uid']."<br>" . $row['date']."<br>".$row['message']."<br><br>". "</p></div>";
}
It should work perfectly.
Another option is to close php tag (?>) and use html markup with <?=$var?>:
<?php
$sql = "SELECT * FROM comments";
$result = OpenCon()->query($sql);
while( $row = $result->fetch_assoc()) {?>
<div class="card">
<p class="card-text">
<?=$row['uid']?><br>
<?=$row['date']?><br>
<?=$row['message']?><br><br>
</p>
</div>
<?php
} // end while
I'm creating a news website, and when I create a new article, the title/text are registered on the database. The articles are in a dynamic PHP page with header/footer, with the text itself being called from the database . On the index there is a list with links to every article, and I want those links to change the id automatically.
index.php
<?php include "header.php"; session_start();?>
<?php include "slideshow.php"; ?>
<?php include "db.php"; ?>
<br>
<table class="table_index">
<?php
while ($line = mysqli_fetch_array($query)) {
echo '<tr><td class="td_tit"><a href="example_article.php" >'.$line["title_article"].'</a></td></tr>';
echo '<tr><td class="td_subt"><a href="example_article.php" >'.$line["subtitle_article"].'</a></td></tr>';
$_SESSION['id_article'] = $line["id_tb_article"];
}
?>
</table>
<?php include "footer.php"; ?>
example_article.php
<?php
session_start();
include "header.php";
$id_article = $_SESSION['id_article'];
$query = "SELECT title_article, subtitle_article, content_article FROM tb_article WHERE id_tb_article = $id_article";
$conn = mysqli_connect('127.0.0.1:3307', 'root', '', 'article') or die("error");
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<div class='titlediv'><h1 class='title'>" . $row["title_article"]. "</h1></div><div class='titlediv'><h3 class='title'>". $row["subtitle_article"]. "</h3></div><div class='textdiv'><p class='text'>" . $row["content_article"]. "</p></div><br>";
}
} else {
echo "0 results";
}
include "footer.php";
?>
You shouldn't use session parameters for this task. Instead use get parameters (or post) here is a little example. Also your code is vulnerable to SQL-Injection, but thats your problem :D
Index.php rewrite your while loop like this
while ($line = mysqli_fetch_array($query)) {
echo '<tr><td class="td_tit">'.$line["title_article"].'</td></tr>';
echo '<tr><td class="td_subt">'.$line["subtitle_article"].'</td></tr>';
}
then fetch the id in example_article.php like this:
$id_article = $_GET['id_article'];
<?php
$conn=mysqli_connect("localhost","id6755695_artemi8","sharanod"
,"id6755695_user_info");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$department = $_POST["department"];
$year = $_POST["year"];
$sem = $_POST["semester"];
$reg = $_POST["regulation"];
$sql = "SELECT book_name, author, edition, image FROM dept_search WHERE
department='$department' AND year='$year' AND semester='$sem' AND
regulations='$reg' ";
$result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
if($row)
{
echo "<br>";
?>
<img src=" <?php echo $row['image']; ?> " height="300" width="300">
<?php
echo "<br>";
echo "<b>",$row['book_name'],"</b>";
echo "<br>";
echo $row['author'];
echo "<br>";
echo $row['edition'];
}
else
{
echo "sorry book not found";
}
}
mysqli_close($conn);
?>
please help me with this code,i am building a library management system.. The thing is I should be able to display the books if the given values are present i have in the database if not book not found must be displayed but in while loop after if, else does not runs.....
As others have pointed out, your else statement will never run. If you are already inside the while loop, you will certainly have $row defined and for that reason, else will never run.
What you can do is, check beforehand if the query returned actual results, like so:
$result=mysqli_query($conn,$sql);
if($result->num_rows > 0){
while($row = mysqli_fetch_assoc($result)){
echo "<br>";
?>
<img src=" <?php echo $row['image']; ?> " height="300" width="300">
<?php
echo "<br>";
echo "<b>",$row['book_name'],"</b>";
echo "<br>";
echo $row['author'];
echo "<br>";
echo $row['edition'];
}
}else{
echo "Sorry book not found";
}
You can try with mysqli_num_rows .. sample code as follows :
$rowcount=mysqli_num_rows($conn,$sql);
if($rowcount!=0){
$result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
echo "<br>";
?>
You are looping through all the rows returned from the "mysqli_fetch..." command. Your "if" and "else" is useless -- you will always have rows. If you get no rows, you do not even enter the body of the while loop.
You need to COUNT the rows returned (count($row)) and display a message that nothing was found if the count is less than one.
All you need to do is that you have to change if the condition from if($row) to if($other_condition)
Currently, you are just checking either there is something inside $row, and this condition will never be wrong unless you will assign it null. Because where $row will have something then while loop will be executed, and when while loop will be executed then if condition will be executed.
you have to simply one thing, that is to change if condition like given below...
if($row['value'] == 'something')
I have two Mysql tables "addcoupons" with one column(couponvalue) and two rows with values 50 and 100. Another table "paytoget" with one column(usercouponinhand).
I been trying to get all the rows in table-1 to a html page and pass the particular id of the row when user click on its value to anther php page where it inserts it in table-2. But unfortunately i have missed something which causing it to print id of first row irrespective of which value user clicks.
My code goes as:
Page 1 (user interaction page)
<br>
<?php
$sql2 = "SELECT * FROM addcoupons WHERE customernumber = '$mobile' ";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
$index = 0;
while($row2 = $result2->fetch_assoc()) {
$index++;
?>
<form action='usercouponadd.php?id=<?php echo $row2['id'];?>' method="post" id="usercouponadd">
<div>
<h4 class="amount" form="usercouponadd"> <strong>Rs. <?php echo ($row2["couponvalue"]); ?></strong></h4>
<button class="addbutton" form="usercouponadd" type="submit"><span> <strong>Add</strong> </span></button>
</div>
<hr class="line" align="left">
<?php }
} else {
echo "None";
}
?>
</div>
The second php page (insert into table-2)
$mobile = $_SESSION['mobile'];
$date = date('M-d,Y H:i:s');
$date2 = date('M-d,Y');
$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql2 = "SELECT * FROM addcoupons WHERE id = " .$_GET["id"];
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
$index = 0;
while($row2 = $result2->fetch_assoc()) {
$index++;
$usercouponinhand = $row2["couponvalue"];
$sql = "INSERT INTO userpaytoget (usercouponinhand, mobile, date, date2)
VALUES ('$usercouponinhand', '$mobile', '$date', '$date2')";
if ($conn->query($sql) === TRUE) {
echo '<script language="javascript">';
echo 'alert("coupon has been successfully added")';
echo '</script>';
}
else {
echo "ERROR" . $sql . "<br>" . $conn->error;
}
}
} else {
echo " ";
}
$conn->close();
Table-1 "addcoupons"
couponvalue
50
100
Table-2 "paytoget"
usercouponinhand
xx
xx
Any Help is Appreciated....
I guess i figured out the answer for "my" problem.
I just removed the form tag since i m not submitting any data from user, and added that addcoupon.php link to anchor link. it worked talking the particular id which user clicks..
Code goes like this::
<div>
<h4 class="amount" form="usercouponadd"> <strong>Rs. <?php echo ($row2["couponvalue"]); ?></strong></h4>
<a href="usercouponadd.php?id=<?php echo $row2['id'];?>" class="addbutton" >Add</a>
</div>
Thanks to all who took their time to give this problem a glance...
I have the following code to load users in a table: register.php here I want to delete users from. Only the second script deleteUsers.php doesn't work.
Can any body help me out?
register.php`
<?php
$edit = 'edit:';
$account = 'Username:';
$account = '<font size="4">'.$account.'</font>';
$password1 = 'Password:';
$password1 = '<font size="4">'.$password1.'</font>';
$id = 'id:';
$id = '<font size="4">'.$id.'</font>';
//check db connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Take everything from table and fill in $result
$sql = "SELECT * FROM login";
$result = $conn->query($sql);
echo"<table border=4><tr><td>$account</td><td>$password1</td><td>$id</td><td>edit</td><td>delete</td></tr>";
if ($result->num_rows > 0) {
// Take all data
while($row = $result->fetch_assoc()) {
echo"<tr><td>".$row['username']."</td><td>".$row['password']."</td><td>".$row['id']."</td><td><a href='/editUser.php'> edit </a> </td><td> <a href='/deleteUser.php'> delete</a> </td></tr>";
}
} else {
// nothing in DB is 0 results
echo "0 results";
}
echo"</table>";
$conn->close();
?>
You see there is an delete button who redirect to deleteUser.php
This is the page you find here below, unfortunately the delete function doesn't work:
<?php
require_once("/db_conn.php");
// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
// get id value
(isset($_GET['id'])
// delete the entry
$result = mysql_query("DELETE FROM login WHERE id =$id")
or die(mysql_error());
// redirect back to the view page
header("Location: register.php");
}
else
// if id isn't set, or isn't valid, redirect back to view page
{
echo "doesn'twork";
}
?>
You need to pass the id variable :
while($row = $result->fetch_assoc())
{
echo"<tr><td>".$row['username']."</td><td>".$row['password']."</td><td>".$row['id']."</td><td><a href='/editUser.php'> edit </a> </td><td> <a href='/deleteUser.php?id=" . $row['id'] . "'> delete</a> </td></tr>";
}
you need to pass the id
while($row = $result->fetch_assoc()) {
echo"<tr><td>".$row['username']."</td><td>".$row['password']."</td><td>".$row['id']."</td><td><a href='/editUser.php'> edit </a> </td><td> <a href='/deleteUser.php?id=". $row['id']."'> delete</a> </td></tr>";
}
on deleteuser.php
$id=$_GET['id'];
$result = mysql_query("DELETE FROM login WHERE id =$id")
or die(mysql_error());
$id=$_GET['id']; must put after query