How should I appear the image in the page? - php

Where should I place a the image enable to appear, and what code should be insert I mean how to put some ('admin/savephp/images/<?php echo $rows['image']; ?>') because this is the place where the image was place
<?php
include('../connect.php');
$result = $db->prepare("SELECT * FROM candposition ORDER BY posid ASC");
$result->bindParam(':userid', $res);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$dsds=$row['posid'];
$resulta = $db->prepare("SELECT sum(votes) FROM candidates WHERE posid= :a");
$resulta->bindParam(':a', $dsds);
$resulta->execute();
for($i=0; $rowa = $resulta->fetch(); $i++){
$dsada=$rowa['sum(votes)'];
}
echo '<div style="margin-top: 18px;">';
echo '<strong>'.$row['pos_name'].' '.'</strong><br>';
$results = $db->prepare("SELECT * FROM candidates,users WHERE candidates.idno=users.idno AND posid= :a ORDER BY votes DESC");
$results->bindParam(':a', $dsds);
$results->execute();
for($i=0; $rows = $results->fetch(); $i++){
if($dsds=='Commissoner'){
echo $rows['course'].' '.$rows['name'].' = '.$rows['votes'];
} else {
echo $rows['lastname'].' = '.$rows['votes'];
}
$sdsd=$dsada
?>
<img src="../img/percent.gif"width='<?php echo(100*round($rows['votes']/($sdsd),2)); ?>'height='10'>
<?php
echo(100*round($rows['votes']/($sdsd),2));
?>
%<br>
<?php
}
}
?>

You can do this whereever you need the image
echo '<img src="admin/savephp/images/'.$rows['image'].'" \>';

Related

How can echo 1 div differently from mysql data?

I have some sort of timeline on my page, and I am getting 3 late news from database like that:
<div id="timeline">
<?php
$SQLGetNews = $odb -> query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 3");
while ($getInfo = $SQLGetNews -> fetch(PDO::FETCH_ASSOC)) {
$name = $getInfo['name'];
/*It should be like that
<div class="left">'.$name.'</div>
<div class="right">'.$name.'</div>
I know that i have to echo it somehow by targeting first, second, third output?
*/
}
?>
</div>
So you want to print info like left then right then left aligned, You can do this like below.
<div id="timeline">
<?php
$SQLGetNews = $odb -> query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 3");
$i=1;
while ($getInfo = $SQLGetNews -> fetch(PDO::FETCH_ASSOC)) {
$name = $getInfo['name'];
if($i%2)
{
echo '<div class="right">'.$name.'</div>';
}
else
{
echo '<div class="left">'.$name.'</div>';
}
$i++;
/*It should be like that
<div class="left">'.$name.'</div>
<div class="right">'.$name.'</div>
I know that i have to echo it somehow by targeting first, second, third output?
*/
}
?>
</div>
This should illustrate something you could do to alternate between left and right while iterating.
function isEven(int $num):bool{
return $num % 2 == 0;
}
$len = count($names = ['foo','bar','fez']);
$i = 0;
while($i < $len){
$name = $names[$i];
$class = "right";
if(isEven($i)){
$class = "left";
}
echo $name . " is on the " . $class . "\n";
$i++;
}
// foo is on the left
// bar is on the right
// fez is on the left
<div id="timeline">
<?php
$SQLGetNews = $odb -> query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 3");
$i=1;
while ($getInfo = $SQLGetNews -> fetch(PDO::FETCH_ASSOC)) {
$name = $getInfo['name'];
if($i%2)
{
?>
<div class="right"><?php echo $name; ?></div>
<?php
}
else
{
?>
<div class="left"><?php echo $name; ?></div>
<?php
}
$i++;
/*It should be like that
<div class="left">'.$name.'</div>
<div class="right">'.$name.'</div>
I know that i have to echo it somehow by targeting first, second, third output?
*/
}
?>
</div>

php pagination with search not working together?

This is the small minor project from college. It ranks the students as per the no of votes. It does rank the student in the pagination but when the student profile is clicked the rank is not shown.
Ok so the below code works but the last part of the code does not display the rank in the output. how can I $num in both codes?
HOME
<br><br>
<form>
<input type="text" name="id" autocomplete="off" placeholder="enter student id">
</form>
<br><br>
<?php include 'conn.php'; ?>
<?php
$sql = "SELECT * FROM table";
$result = mysqli_query($conn, $sql);
$result_per_page = 5;
$no_of_result = mysqli_num_rows($result);
$no_of_page = ceil($no_of_result/$result_per_page);
if(!isset($_GET['page'])){
$page = 1;
}else{
$page = $_GET['page'];
}
$page_first = ($page-1)*$result_per_page;
$sql = 'SELECT * FROM table ORDER BY votes DESC LIMIT ' . $page_first . ',' . $result_per_page;
$result = mysqli_query($conn, $sql);
$num = $page * $result_per_page -4;
while($row = mysqli_fetch_assoc($result)) {
echo '<div class="x">';
echo '#';
echo $num++;
echo ' ';
echo '<a href="?id='.$row['id'].'">';
echo $row['name'];
echo '</a>';
echo '</div>';
}
echo '<br>';
for($page=1;$page<=$no_of_page;$page++){
echo ''.$page.'';
echo ' ';
}
echo '<br>';
?>
<?php
$name = $_GET['id'];
$sql = "SELECT * FROM table WHERE id=$name";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo '<br>';
echo 'RANK = ';
echo '?';//display rank here?
echo '<br>';
echo 'NAME = ';
echo $row['name'];
echo '<br>';
echo 'VOTES = ';
echo $row['votes'];
echo '<br>';
}
} else {
//echo "0 results";
}
?>
The output of the above code.. I want to output the rank in the place of question mark.
current output
i want something like this
desired output

php pagination code is not working

Can anyone point me out why my pagination is not working, What I am doing wrong here ? Working on it for a long time. my url generates like : http://localhost/medapp/admin/medorder.php?page=%209.
<?php
//pagination
$perpage = 3;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
$start_from = ($page-1)*$perpage;
//pagination
$medorder = "SELECT * FROM `medorder` WHERE status='1' order by ID desc";
$result = $db->select($medorder);
if($result){
$i=0;
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>".$i++."</td>";
echo "<td>".$row["uid"]."</td>";
echo "<td>".$row["fullname"]."</td>";
echo "</tr>";
}
// pagination
$query = "select * from medorder";
$result = $db->select($query);
$total_rows = mysqli_num_rows($result);
$total_pages = ceil($total_rows/$perpage);
echo "<span class='pagination'><a href='medorder.php?page=1'>".'First Page'."</a>";
for ($i=1; $i <= $total_pages; $i++) {
echo "<a href='medorder.php?page=".$i."'>".$i."</a>"; }
echo "<a href='medorder.php?page=$total_pages'>".'Last Page'."</a></span>";
//pagination
}
?>
Change your query like this by using LIMIT and OFFSET
$medorder = "SELECT * FROM `medorder` WHERE status='1' order by ID desc LIMIT $start_from,$perpage"; //
in this following line, you have a space before the $i variable:
echo "<a href='medorder.php?page= ".$i."'>".$i."</a>";
should be
echo "<a href='medorder.php?page=".$i."'>".$i."</a>";

Using keyword LIMIT in MySQL

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("data_wis1") or die(mysql_error());
if(isset($_GET["id"])) {
$id = $_GET["id"];
$sql = "DELETE FROM info WHERE ID = '".$id."'";
mysql_query($sql) or die(mysql_error());
}
if(isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page = 1;
}
$start_from = ($page-1) * 1;
$sql = "SELECT * FROM info LIMIT 0, 1";
$query = mysql_query($sql) or die(mysql_error());
?>
<html>
<table>
<?php
for($i = 0; $i <mysql_num_rows($query); $i++) {
$id = mysql_result($query, $i, "ID");
$caseStatus = mysql_result($query, $i, "Case_Status");
echo "<tr>";
echo "<td>".$id."</td>";
echo "<td>".$caseStatus."</td>";
echo "<td><a href='del.php?id=".$id."'><input type='button' value='Delete'></a></td>";
echo "</tr>";
}
?>
</table>
<?php
$sql = "SELECT COUNT(ID) FROM info";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_row($query);
$total_records = $row[0];
$total_pages = ceil($total_records / 1);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='del.php?page=".$i."'>".$i."</a> ";
}
?>
</html>
My problem is that whenever i click page 2,3,4... the display doesn't change. Page 2,3,4 gets the display of page 1. It should be like this. For example, page 1 should display ID = 1 and case status = open. Page 2 should display ID = 2 and case status = close and so on.
You never used $start_from in anywhere. I think $sql = "SELECT * FROM info LIMIT '".$start_from."', 1"; is the answer

i got error while coding of pagination in php5?

i am writing code of pagination & i got an error.
this is my code :
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("Admin") or die(mysql_error());
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * 2;
$sql = "SELECT * FROM events ORDER BY event ASC LIMIT $start_from, 2";
$result = mysql_query ($sql) or die(mysql_error());
$num=mysql_numrows($result);
$x=0;
?>
<table>
<tr><td>Event</td><td>Types</td></tr>
<?php
while ($x<$num) {
$row1 = mysql_result($result,$x,'event');
$row2 = mysql_result($result,$x,'types');
?>
<tr>
<td><? echo $row1; ?></td>
<td><? echo $row2; ?></td>
</tr>
<?php
$x++;
}
?>
</table>
<?php
$sql = "SELECT COUNT(event) FROM events";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_row($result);
$total_records = $row[0];
$total_pages = ceil($total_records / 2);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
}
?>
now i got an error.
object not found
Note that instead of terrible while ($x<$num) you should use way more neat while($row=mysql_fetch_array($res))
so, make it
<?
$per_page = 2;
$page = 1;
if (isset($_GET['page'])) $page = $_GET['page'];
$start_from = ($page-1) * $per_page;
$sql = "SELECT * FROM events ORDER BY event ASC LIMIT $start_from, $per_page";
$res = mysql_query ($sql) or trigger_error(mysql_error().": ".$sql);
?>
<table>
<tr><td>Event</td><td>Types</td></tr>
<?php
while($row=mysql_fetch_array($res)) {
?>
<tr>
<td><? echo $row['event'] ?></td>
<td><? echo $row['types'] ?></td>
</tr>
<?php
}
?>
</table>
<?php
$sql = "SELECT COUNT(event) FROM events";
$res = mysql_query ($sql) or trigger_error(mysql_error().": ".$sql);
$row = mysql_fetch_row($res);
$total_records = $row[0];
$total_pages = ceil($total_records / $per_page);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
}
?>
mysql_query returns false - i.e. your query failed and you are passing a wrong argument to mysql_num_rows. I think you have a typo in the table name in the SQL query, it should be events instead. Generally, you should check if the query succeeded:
$result = mysql_query ($sql) or trigger_error(mysql_error().": ".$sql);

Categories