Show variable outside while loop - while-loop

Hy. I know there are answers similar, but i can't get it going. Here is my cod:
<?php include 'header.php'; ?>
<?php include 'connect.php';
$q = mysqli_query($link, $aaa = 'SELECT * FROM agentii LEFT JOIN orase ON agentii.oras=orase.oras_id WHERE orase.oras_id = \'' .$_GET['id']. '\'') or die(mysqli_error($link));
$row = mysqli_fetch_assoc($q);
?>
<h2>Title <em><?php echo $row['oras'];?></em></h2>
<div class="div_view">
<table cellspacing="0" cellpadding="5" border="0" width="100%">
<tr>
<th>Agentie</th>
<th>Adresa</th>
</tr>
<?php while ($row = mysqli_fetch_assoc($q)) { ?>
<tr>
<td><?php echo $row['agentie'];?></td>
<td><?php echo $row['adresa'];?></td>
</tr>
<?php }?>
</table>
</div>
<?php include 'footer.php'; ?>
This output(i can't put images because is a new account - so i put a link to a prt src):
http://postimage.org/image/w9zsexz91/
In my database i have 3 rows, and the code outputs only 2.
I know that is because of the 2 mysqli_fetch_assoc, but i want to show all the rows, and i want to show the title too (<?php echo $row['oras'];?>) outside the while.
Can someone please help me with this?
Thx in advance!

obligatory warning first: Remember little Bobby Tables!
(or: your code is vulnerable to sql injection attacks)
then: you get only two rows in the while loop, because you requested a row outside the loop first, thus increasing the internal cursor in the result.
If you want to get title and rows in one go, you need to cache the result, for example:
$resultArray = array();
while ($row = mysqli_fetch_assoc($q)) {
$resultArray[] = $row;
}
//....
?>
<h2><?php echo $resultArray[0]['oras']; ?></h2>
<?php // ... ?>
<?php foreach($resultArray as $row) {
<tr>
<td><?php echo $row['agentie'];?></td>
<td><?php echo $row['adresa'];?></td>
</tr>
} ?>
this is just a stub, of course you should implement checks if you got a row at all etc...

Try:
<?php do { ?>
<tr>
<td><?php echo $row['agentie'];?></td>
<td><?php echo $row['adresa'];?></td>
</tr>
<?php } while ($row = mysqli_fetch_assoc($q)); ?>
The first iteration of this loop will print the first fetched row. I have only changed the location of the while clause, forcing the code of the loop to be run before a new row is fetched.

Related

Am having difficulty displaying data in a table using SQL Aggregate Function SUM()

I tried to use the SQL Aggregate Function SUM () and it successfully but i can't seem to be able to display this in a table
Below is my php code i tried it with
`$sql = " SELECT product_name, sum(unit_price_before_discount)
FROM transaction_sell_lines
GROUP BY product_name";
<!-- TABLE CONSTRUCTION -->
<table>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Current Value</th>
</tr>
<!-- PHP CODE TO FETCH DATA FROM ROWS -->
<?php
// LOOP TILL END OF DATA
while($rows=$result->fetch_assoc())
{
?>
<tr>
<!-- FETCHING DATA FROM EACH
ROW OF EVERY COLUMN -->
<td><?php echo $rows['product_name'];?></td>
<td><?php echo $rows['quantity'];?></td>
<td><?php echo $rows['unit_price_before_discount'];?></td>
<td><?php echo ($rows['unit_price_before_discount'])*$rows['quantity'];?></td>
</tr>
<?php
}
?>
</table>`
This is the result i get
And this is my database structure
What i want to do is to add all the values in quantity that has the same name together and display in a table

How to display user_nicename and usermeta values by custom query in Wordpress?

I am trying to display user_nicename and one of usermeta values where meta_key is description. My query is given below:
<?php
$querystr = "
SELECT *
FROM $wpdb->users, $wpdb->usermeta
WHERE $wpdb->users.ID = $wpdb->usermeta.user_id
AND $wpdb->usrmeta.meta_key = 'description'
";
$retrieve_data = $wpdb->get_results($querystr, OBJECT);
?>
<?php global $retrieved_data; foreach ($retrieve_data as $retrieved_data){ ?>
<table>
<tr>
<th>Customer Name</th>
<th>Customer Details</th>
</tr>
<tr>
<td><?php echo $retrieved_data->user_nicename;?></td>
<td><?php echo $retrieved_data->meta_value;?></td>
</tr>
</table>
<?php } ?>
My final output should be displayed inside a table which will look something like below:
But I am in confusion how to do that. I am facing problem for both sql syntax and displaying data inside the table. Sorry for this easy question, I am still a learner. Can anyone help me in here? Thanks

pdo count not working

I have a page and all the function is working my only problem now is ciunying the record from the databse..
Class.user.php
Public function data($count)
{
$stmt=$this->db->prepare("SELECT COUNT(*) FROM login");
$result=$this->db->prepare($count);
$result->execute();
$number_of_rows=$result->fetchColumn();
}
Index.php
<table>
<thead>
<tr>
<th>2014</th>
</tr>
</thead>
<tbody>
<?php
$count="SELECT COUNT(*) FROM login";
$crud->data($count);
?>
The problem is that its not showing the count..
You're writing the same query as function argument, but also inside the function itself. And you're only really using one. This is nonsense. Dedicate your method to return the count, don't make the query a parameter.
prepareing the statement is pointless in this case, since you're neither reusing it nor are you binding any values. You can simply query() it directly.
The clincher: you're neither outputting nor returning the count, so it's very very expected that it doesn't show up anywhere.
Here's a sane version:
public function getCount() {
$result = $this->db->query('SELECT COUNT(*) FROM login');
return $result->fetchColumn();
}
<tbody>
<tr>
<td>
<?php echo $crud->getCount(); ?>
I think I usually do it like this link
$sql= "SELECT COUNT(*) FROM login";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$row =$stmt->fetchObject();
then to output it you would have to ECHO
<table>
<tbody>
<tr>
<td>
<?php echo $row['count'];?>
</td>
</tr>
</tbody>
</table>

Deleting single item with submit from database results in two ID's and strings

When I try and delete an entry from the code below it seems to spew out two array values with different index numbers and will not delete the entry. If I click multiple times it finally gets the correct ID but only by some random chance.
Using echo var_dump($deleted) results in something like:
String(2) "10"
String(1) "7"
These can vary and seems to be random.
I have reduced my whole plugin to just the code below thinking that maybe some other submit query might be interfering with it. And still I get two ID's with unknown string assignments.
Any ideas?
function myplugin_update_page(){
global $wpdb;
$update_page_list = $wpdb->get_results('SELECT * FROM wp_update_page');
$active = $wpdb->get_var("SELECT * FROM wp_update_page WHERE active='on'");
echo '<form method="post" action=""><table>';
foreach ( $update_page_list as $update ) { ?>
<tr>
<td><input type="image" src="<?php echo plugin_dir_url(__FILE__).'images/delete.png'; ?>" name="delete[]" value="<?php echo $update->ID; ?>" /></td>
</tr>
</table>
</form>
</div>
<?php
}
if(isset($_POST['delete'])) {
$delete = $_POST['delete'];
foreach ($delete as $deleted) {
$wpdb->query("DELETE FROM wp_update_page WHERE ID = $deleted");
}
echo "<script type='text/javascript'>window.location=document.location.href;</script>";
}
}

Auto navigate (scroll) to certain table row

I have a table with few thousand records on few pages in a simple html table.. I made a search function that works fine apart from one thing... It displays only one result in a table (which is great cause it means it works!).But... I was wondering is there a way to display back the table with all records, with the one that i was searching for in the middle and highlighted? Here's a simplified table that I have :
<table class="nogap" cellpadding="1" bgcolor="#00000" cellspacing="1" style="margin:110px 0 0 5px; width:100%; border-color:#B6D6F6;" >
<tbody>
<?php include 'dbconn.php';?>
$con = mysqli_connect($host,$user,$pass,$db) or (header( 'Location: errorpage.php' ));
if (mysqli_connect_errno($con)) { header( 'Location: errorpage.php' ); }
$sql = "SELECT * FROM $tb1 ORDER BY (Serial_num +1) LIMIT $offset, $rowsperpage";
$result = mysqli_query($con, $sql) or (header( 'Location: errorpage.php' ));
$row = mysqli_num_rows($result);
while ($row = $result->fetch_assoc())
{
$product = $row['Prod_type'].$row['Serial_num'];
<tr id="mstrTable" class="lovelyrow">
<td width="5%"><?php echo $product;?></td>
<td width="5%"><?php echo $row['Customer'];?></td>
<td width="7%">
<a href="#"
onmouseover="ajax_showTooltip(window.event,'getptn.php?prd=<?php echo $p;?>',this);return false"
onmouseout="ajax_hideTooltip()">
<?php echo$row['Prod_info'];?>
</a>
</td>
</tr>
}
</table>
Thanks!
First of all don't give the same html id attribute to each row (mstrTable). Html Ids should be unique per page.
Instead mark table rows with unique ids, eg:
$html .= "<td id='row_".$row['id']."'>"
Then do a search query first, remember item id, figure out what page should be queried, query the whole page, attach classess 'greyedout' and 'highlighted' to rows accordingly and then you might try this javascript function to scroll down to the item:
https://developer.mozilla.org/en-US/docs/Web/API/element.scrollIntoView