Skip row with particular id - php

I want to skip one record from mysql using php when that record match with a variable value.
The $row['uni_id'] is id in a row of my database table and $_SESSION['uid'] stores a id in session. When the ids match, that record should be skipped. But it's not working. Do you see the issue?
while($row = mysql_fetch_array($res,MYSQL_ASSOC )){
if ($row['uni_id'] == $_SESSION['uid']){
continue;
}
$_SESSION['new_img']=$row['pic_add'];enter code here
$image= $row['pic_add'];
echo "<tr><td>";
echo "{$row['fname']} {$row['lname']}";
echo "</td><td>";
echo $row['street'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
}
mysql_free_result($res);

Can you try the below code ?
<?php
while($row = mysql_fetch_array($res,MYSQL_ASSOC )){
if ($row['uni_id'] != $_SESSION['uid']){
$_SESSION['new_img'] = $row['pic_add'];
$image= $row['pic_add'];
echo "<tr><td>";
echo "{$row['fname']} {$row['lname']}";
echo "</td><td>";
echo $row['street'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
}
}
mysql_free_result($res);
?>

I think you are using condition in a wrong manner.
Below code might help you.
while($row = mysql_fetch_array($res,MYSQL_ASSOC )){
if ($row['uni_id'] == $_SESSION['uid']){
// do nothing
}
else{
$_SESSION['new_img']=$row['pic_add'];
$image= $row['pic_add'];
echo "<tr><td>";
echo "{$row['fname']} {$row['lname']}";
echo "</td><td>";
echo $row['street'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
}
}
mysql_free_result($res);

You should adjust your MySQL query before you start processing the result in PHP.
The solution will depend on your query. If your query is something like this:
select * from table
You should update it to:
select * from table WHERE uid <> "$_SESSION['uid']"
If you cannot control the MySQL query, then you can change your PHP code:
while($row = mysql_fetch_array($res,MYSQL_ASSOC )){
if ($row['uni_id'] != $_SESSION['uid']){
$_SESSION['new_img']=$row['pic_add'];enter code here
$image= $row['pic_add'];
echo "<tr><td>";
echo "{$row['fname']} {$row['lname']}";
echo "</td><td>";
echo $row['street'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
}
}
mysql_free_result($res);

Try this
while($row = mysql_fetch_array($res,MYSQL_ASSOC )){
if ($row['uni_id'] != $_SESSION['uid']){
$_SESSION['new_img']=$row['pic_add'];
$image= $row['pic_add'];
echo "<tr><td>";
echo "{$row['fname']} {$row['lname']}";
echo "</td><td>";
echo $row['street'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
}
}
mysql_free_result($res);

while($row = mysql_fetch_array($res,MYSQL_ASSOC )){
if ($row['uni_id'] !== $_SESSION['uid']){
$_SESSION['new_img']=$row['pic_add'];
$image= $row['pic_add'];
echo "<tr><td>";
echo "{$row['fname']} {$row['lname']}";
echo "</td><td>";
echo $row['street'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
}
}
mysql_free_result($res);
NOTE: Do not use mysql. Its deprecated. Use PDO.

Try this code fragment
while($row = mysql_fetch_array($res))
{
$t='';
if($row['uni_id'] != $_SESSION['uid'])
{
$_SESSION['new_img']=$row['pic_add'];//enter code here
$image= $row['pic_add'];
$t.="<tr>";
$t.="<td>{$row['fname']} {$row['lname']}</td>";
$t.="<td>".$row['street']."</td>";
$t.="<td>".$row['city']."</td>";
$t.="</tr>";
}
echo $t;
}
mysql_free_result($res);

Related

Merge table cell in html view from phpexcel

How to add rowspan/colspan from phpexcel view table in html ?
I have this table.
I want to view that excel in html using PHPexcel View
but what i got is just like,
The first column was not merged with the second column.
Does anyone know how to add rowspan in phpexcel view in html ?
My code is just like this
$tmpfname = "./sampleData/member.xls";
$excelObj = $excelReader->load($tmpfname);
$worksheet = $excelObj->getActiveSheet();
$worksheet->mergeCells('A1:A3');
$cell = $worksheet->getCell('A1');
$lastRow = $worksheet->getHighestRow();
echo '<table border="1" >';
for ($row = 1; $row <= $lastRow; $row++) {
echo "<tr><td>";
echo $worksheet->getCell('A'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('B'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('C'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('D'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('E'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('F'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('G'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('J'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('K'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('L'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('M'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('N'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('O'.$row)->getValue();
echo "</td><tr>";
}
echo "</table>";
Probably you can use createWriter for creating HTML table from phpExcelObject.
$objWriter = $this->get('phpexcel')->createWriter($phpExcelObject, 'HTML');
ob_start();
$objWriter->save('php://output');
$excelOutput = ob_get_clean();
echo $excelOutput;

PHP Button href not working

For a school project I am trying to create a shoppingcart with php only with MySQLi. For this I have a catalogue called index.php. In this is a table with the product and after every product there is a button which should add the item to the shoppingcart.
The only problem is that I cannot get the link working properly.
<?php
session_start();
include 'connect.php';
$qry = "select * from products";
$result = mysqli_query($connect, $qry);
echo "<table class='catalogue'>";
echo "<tr><th>ID</th><th>Code</th><th>Name</th><th>Description</th><th>Image</th></th><th>Price</th><th>Buy</th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['product_code'];
echo "</td><td>";
echo $row['product_name'];
echo "</td><td>";
echo $row['product_desc'];
echo "</td><td>";
echo $row['product_img_name'];
echo "</td><td>";
echo $row['price'];
echo "</td><td>";
echo "<input type='submit' value='Add' href='cart.php?id=['id']'/>";
echo "</td></tr>";
}
echo "</table>";
?>
The cart.php looks like this.
<?php
session_start();
require 'connect.php';
require 'item.php';
$result = mysqli_query($connect, 'select * from products where id='.$_GET['id']);
$product = mysqli_fetch_object($result);
if(isset($_GET['id'])){
$item = new Item();
$item->id = $product->id;
$item->name = $product->product_name;
$item->price = $product->price;
$item->quantity = 1;
$_SESSION['cart'][] = $item;
}
echo "<table class='cart'>";
echo "<tr><th>ID</th><th>Name</th><th>Price</th><th>Quantity</th><th>Sub Total</th></tr>";
$cart = unserialize(serialize($_SESSION['cart']));
for($i=0; $i<count($cart); $i++){
echo "<tr><td>";
echo $cart[$i]->id;
echo "</td><td>";
echo $cart[$i]->product_name;
echo "</td><td>";
echo $cart[$i]->price;
echo "</td><td>";
echo $cart[$i]->quantity;
echo "</td><td>";
echo $cart[$i]->price * $cart[$i]->quantity;
echo "</td></tr>";
}
?>
Please forgive any other mistakes you might see, I am rather new to PHP.
Buttons don't have hrefs, anchors(<a>) do, so using an anchor it would be
echo "<a href='cart.php?id=$row[id]'/>Add</a>";
you could always style it like a button if you want it to look like one.

Use data from mysql in HTML row

I have a web page where I am pulling data from a mysql database and I want to be able to check a checkbox and change information within the table. The way I am displaying the information is as follows:
<?php
$get_tickets = mysql_query("SELECT * FROM assignment
JOIN technician
ON assignment.TechID = technician.TechID
JOIN employee
ON assignment.EID = employee.EID
JOIN ticket
ON assignment.TickID = ticket.TickID
WHERE assignment.TechID = '$current_id'");
echo "<table border='1' width=\"100%\" style=\"margin: 0px;\">";
echo "<tr><td>Ticket Number</td>";
echo "<td>First Name</td>";
echo "<td>Last Name</td>";
echo "<td>Phone</td>";
echo "<td>Device</td>";
echo "<td>Location</td>";
echo "<td>Problem</td>";
echo "<td>Time Stamp</td>";
echo "<td>Completed</td>";
echo '<td><form><input type="checkbox"/></form></td></tr>';
while($row2 = mysql_fetch_array($get_tickets))
{
$count = 1;
$checkbox = "checkbox" . $count;
echo "<tr><td>";
echo $row2["TickID"];
echo "</td><td>";
echo $row2["fname"];
echo "</td><td>";
echo $row2["lname"];
echo "</td><td>";
echo $row2["phone"];
echo "</td><td>";
echo $row2["device"];
echo "</td><td>";
echo $row2["location"];
echo "</td><td>";
echo $row2["problem"];
echo "</td><td>";
echo $row2["time_date"];
echo "</td><td>";
echo $row2["completed"];
echo "</td><td>";
echo '<form><input type="checkbox" name=' . $row2["TickID"] . '></form>';
echo "</td></tr>";
}
?>
There may be better ways to ouput the information, but I just want to be able to click the checkbox and take the data from the row associated with the checkbox and change part of it. Any help is greatly appreciated.
try to change this:
echo '<form><input type="checkbox" name=' . $row2["TickID"] . '></form>';
for this:
echo ''.$row2["fname"].'';
on the file process.php you will get the variable like this
<?php
echo $_GET['id'];
?>

How do I stop my sql query from displaying the results twice?

How do I stop the results from getting displayed twice on the web page? What have I done wrong? Yes this is for a game but it helps me learn SQL and PHP. Please be genital it's my first time.
There is one database with two tables. The database is game_tittle the two tables are player_data and the second is character_data.
<?php
include('db_conn.php');
#for connecting to SQL
$sqlget = "SELECT player_data.PlayerName, Character_data.Duration, character_data.KillsZ, character_data.KillsB, character_data.KillsH, character_data.HeadshotsZ, character_data.Humanity, character_data.Generation, character_data.InstanceID FROM player_data, character_data";
$sqldata = mysqli_query($dbcon, $sqlget) or die('error getting data');
echo "<center><table>";
echo "<tr> <th>Player Name</th><th>play time</th><th>Total Kills</th><th>Bandit kills</th><th>Hero Kills</th><th>Head shots</th><th>Humanity</th><th>Alive count</th><th>Instance ID</tr>";
while ($row = mysqli_fetch_array($sqldata)) {
echo "<tr><td>";
echo $row['PlayerName'];
echo "</td><td>";
echo $row['Duration'];
echo "</td><td>";
echo $row['KillsZ'];
echo "</td><td>";
echo $row['KillsB'];
echo "</td><td>";
echo $row['KillsH'];
echo "</td><td>";
echo $row['HeadshotsZ'];
echo "</td><td>";
echo $row['Humanity'];
echo "</td><td>";
echo $row['Generation'];
echo "</td><td>";
echo $row['InstanceID'];
echo "</td></tr>";
echo "</center>";
}
echo "</table>";
#end of table
?>
Got it to work this way.
<?php
include('db_conn.php');
$sqlget = "SELECT player_data.PlayerUID, character_data.PlayerUID, player_data.PlayerName, character_data.HeadshotsZ, character_data.KillsZ, character_data.KillsH, character_data.KillsB, character_data.Alive, character_data.Generation, character_data.Humanity, character_data.InstanceID FROM player_data INNER JOIN character_data ON player_data.PlayerUID = character_data.PlayerUID";
$sqldata = mysqli_query($dbcon, $sqlget) or die('error getting data');
echo "<center><table>";
echo "<tr> <th>Player Name</th><th>Head shots</th><th>Total Kills</th><th>Hero kills</th><th>Bandit Kills</th><th>Live or dead</th><th>Lifes</th><th>Humanity</th><th>Instance ID</tr>";
while ($row = mysqli_fetch_assoc($sqldata)) {
echo "<tr><td>";
echo $row['PlayerName'];
echo "</td><td>";
echo $row['HeadshotsZ'];
echo "</td><td>";
echo $row['KillsZ'];
echo "</td><td>";
echo $row['KillsH'];
echo "</td><td>";
echo $row['KillsB'];
echo "</td><td>";
echo $row['Alive'];
echo "</td><td>";
echo $row['Generation'];
echo "</td><td>";
echo $row['Humanity'];
echo "</td><td>";
echo $row['InstanceID'];
echo "</td></tr>";
echo "</center>";
}
echo "</table>";
#end of table
?>
Bug in your SQL-query: You forgot to join the tables with a "where"-clause.

PHP: How to Insert an Image in a new row for every 10th row in the MySQL?

I would like to insert a new random image in a row (all by itself) after 10 rows of data have been populated.
I'm thinking I should input something like this:
if($counter % 10 == 0) {
echo 'image file';
}
But not really sure how to incorporate this in my code:
echo "<table border='1' CELLPADDING=5 STYLE='font-size:13px'>";
echo "<tr> <td><H3>No.</H3></td><td><H3>Date</H3></td>";
echo "<td><H3>First Name</H3></td> <td><H3>Last Name</H3></td>";
echo "<td><H3>City</H3></td><td><H3>Province</H3></td></tr>";
//declaring counter
$count=0;
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array( $data, MYSQL_ASSOC )) {
//counter equals
$count=$count+1;
// Print out the contents of each row into a table
echo "</td><td>";
echo $count;
echo "</td><td>";
echo $row['DateIn'];
echo "</td><td>";
echo $row['FirstName'];
echo "</td><td>";
echo $row['LastName'];
echo "</td><td>";
echo $row['City'];
echo "</td><td>";
echo $row['Province_State'];
echo "</td></tr>";
}
echo "</table>";
echo "<table border='1' CELLPADDING=5 STYLE='font-size:13px'>";
echo "<tr> <td><H3>No.</H3></td><td><H3>Date</H3></td><td><H3>First Name</H3></td> <td> <H3>Last Name</H3></td> <td><H3>City</H3></td><td><H3>Province</H3></td></tr>";
//declaring counter
$count=0;
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array( $data, MYSQL_ASSOC )) {
//counter equals
$count++;
//insert an image every 10 rows
if($count==10){
$count=0;
echo 'yourimage.jpg';
}
// Print out the contents of each row into a table
echo "</td><td>";
echo $count;
echo "</td><td>";
echo $row['DateIn'];
echo "</td><td>";
echo $row['FirstName'];
echo "</td><td>";
echo $row['LastName'];
echo "</td><td>";
echo $row['City'];
echo "</td><td>";
echo $row['Province_State'];
echo "</td></tr>";
}
echo "</table>";
if($counter % 10 == 0) {
echo 'image file';
}
10, not 100 :). Put this after your $count=$count+1 statement in a td colspan=however many columns you have.
Thanks to the help of Here is the completed code that works (however, I still can't get the random image to show, but I can get an image :-)
echo "<table border='1' CELLPADDING=5 STYLE='font-size:13px'>";
echo "<tr> <td><H3>No.</H3></td><td><H3>Date</H3></td><td><H3>First Name</H3></td> <td><H3>Last Name</H3></td> <td><H3>City</H3></td><td><H3>Province</H3></td></tr>";
//declaring counter for data
$count=0;
//declaring counter for random image
$counter_for_image=0;
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array( $data, MYSQL_ASSOC )) {
//counter for actual count
$count=$count+1;
//counter for image count
//so I can reset count and
//not affect actual count
$counter_for_image++;
/* does not work
// start random image code
$images = array(
0 => '1.jpg',
1 => '2.jpg',
);
$image = $images[ rand(0,(count($images)-1)) ];
$randomimage = "<img src=\"/http://wwww.my site.com/storm/images/".$image."\" alt=\"\" border=\"0\" />";
//print($output);
// end random image code
*/
// start every 10th row image display code
if($counter_for_image==10){
$counter_for_image=0;
echo '<tr><td colspan="6"><center><img src="http://www.mysite.com/storm/images/eric.jpg"/></center></td></tr>';
/* would use this if I could get random image to work
echo '<tr><td colspan="6"><center>';
print($randomimage);
echo '</center></td></tr>';
*/
} // end every 10th row image display code
// Print out the contents of each row into a table
echo "</td><td>";
echo $count;
echo "</td><td>";
echo $row['DateIn'];
echo "</td><td>";
echo $row['FirstName'];
echo "</td><td>";
echo $row['LastName'];
echo "</td><td>";
echo $row['City'];
echo "</td><td>";
echo $row['Province_State'];
echo "</td></tr>";
}
echo "</table>";

Categories