PHP sort database by domain name - php

I have a problem, I have to take the domain name from the database, and create a knokpu with which I can sort the database by this domain, that is, if I clicked on the gmail button, then users with gmail should be sorted, if there are yahoo users then when you click on the yahoo button, the sort will also be performed.
The problem is that I don't know how to properly associate a button with sorting ... I will be very grateful for your help.
Here is my php code.
<!DOCTYPE html>
<html>
<head>
<title>Display all records from Database</title>
</head>
<body>
<h2>All emails</h2>
<table border="2">
<tr>
<td>Sr.No.</td>
<td>E-mail</td>
<td>Delete</td>
<td>CSV</td>
</tr>
<a href="?orderBy=email">
<button>Sort By E-mail</button>
</a>
<a href="?orderBy=date">
<button>Sort By Date</button>
</a>
<a href="?orderBy=">
<button>Export as CSV</button>
</a>
</br>
<?php
include "php/server.php"; // Using database connection file here
$orderBy = array('email', 'date'); // create array of sort options
$order='date'; // default sort option
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
$order = $_GET['orderBy'];
} // change sort option depending on $order
$query = 'SELECT * FROM users ORDER BY '.$order;
$records = mysqli_query($db, $query); // fetch data from database
$dom=array();
include "php/search.php"; //include search engine
if(isset($_POST["submit"])){ //if button is pressed fecth data from search
$records = mysqli_query($db, $searchquery);
}else{ //if not fecth data from db
$records = mysqli_query($db, $query);
}
while($data = mysqli_fetch_array($records))
{
$parts = explode('#', $data['email']);
$domain = array_pop($parts);
?>
<tr>
<td><?php echo $data['email']; ?></td>
<td><?php echo $data['date']; ?></td>
<td>Delete</td>
<td><a><input type="checkbox" id="horns" name="horns"></td>
<td><?php echo $domain; ?></td>
</tr>
<tr>
<input type="submit" name="<?php $domain; array_push($dom,$domain)?>" value="<?php echo $domain; ?>">
</tr>
<?php
}
var_dump($dom);
?>
<h3>Emails is sorting by:<?php echo $order ?></h3>
</table>
</body>
</html>
Design:

I think what you mean is, you want to filter the data based on which the email provider button that clicked?
you can add the HTML like this:
...
<a href="?filterBy=gmail.com">
<button>gmail.com</button>
</a>
<a href="?filterBy=inbox.lv">
<button>inbox.lv</button>
</a>
<a href="?filterBy=yahoo.lv">
<button>yahoo.lv</button>
</a>
...
In PHP you need to update this line:
...
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
$order = $_GET['orderBy'];
}
$filter = "";
if (isset($_GET['filterBy'])) {
$filter = "WHERE `email` LIKE ?";
}
$query = 'SELECT * FROM users '.$filter.' ORDER BY '.$order;
$prepare = mysqli_prepare($db, $query);
if (isset($_GET['filterBy'])) {
$param = "%".$_GET['filterBy'];
$prepare->bind_param('s', $param);
}
$prepare->execute();
// $records = mysqli_query($db, $query); // change this query to:
$records = $prepare->get_result();
...

Related

Unable to delete product from table using PHP

I am in a pickle here. Whenever I try to press the delete button on my project, it goes straight to the PHP file and does nothing to the database that I have set up for it.
Here are some visuals to help you:
Visual number 2
Here is the code for index.php:
<?php
require_once('database.php');
//Get Category Id
$category_id= filter_input(INPUT_GET, 'category_id', FILTER_VALIDATE_INT);
if ($category_id == NULL || $category_id == false){
$category_id = 1;
}
// Get name for selected category
$queryCategory = 'SELECT * FROM categories
WHERE categoryID = :category_id';
$statement1 = $db->prepare($queryCategory);
$statement1->bindValue(':category_id', $category_id);
$statement1->execute();
$category = $statement1->fetch();
$category_name = $category['categoryName'];
$statement1->closeCursor();
//Get all categories
$queryAllCategories = 'SELECT * FROM categories
ORDER BY categoryID';
$statement2 = $db -> prepare($queryAllCategories);
$statement2->execute();
$categories = $statement2->fetchAll();
//Get products fpr selected category
$queryProducts = 'SELECT * FROM products
WHERE categoryID = :category_id
ORDER BY productID';
$statement3 = $db -> prepare($queryProducts);
$statement3 -> bindValue(':category_id', $category_id);
$statement3 -> execute();
$products = $statement3 -> fetchAll();
$statement3 ->closeCursor();
?>
<!DOCTYPE html>
<HTML>
<head>
<title>My Guitar Shop</title>
<link rel="stylesheet" type="text/css" href="../main1.css"/>
</head>
<body>
<header><h1>Product Manager</h1></header>
<main>
<hr>
<h1>Product List</h1>
<aside>
<h2>Categories</h2>
<nav>
<ul>
<?php foreach ($categories as $category) : ?>
<li>
<a href=".?category_id=<?php echo $category['categoryID']; ?>">
<?php echo $category['categoryName'];?>
</a>
</li>
<?php endforeach; ?>
</ul>
</nav>
</aside>
<section>
<!-- display a table of products -->
<h2><?php echo $category_name; ?></h2>
<table>
<tr>
<th>Code</th>
<th>Name</th>
<th class="right">Price</th>
<th> </th>
</tr>
<?php foreach ($products as $product) : ?>
<tr>
<td><?php echo $product['productCode']; ?></td>
<td><?php echo $product['productName']; ?></td>
<td><?php echo $product['listPrice']; ?></td>
<td><form action="delete_product.php" method="post">
<input type="hidden" name="product_id" value="<?php echo $product['productID']; ?>">
<input type="hidden" name="category_id" value="<?php echo $product['categoryID']; ?>">
<input type="submit" value="Delete">
</form></td>
</tr>
<?php endforeach; ?>
</table>
<p>Add Product</p>
</section>
</main>
<hr>
<footer><p>$copy; <?php echo date("Y"); ?> My Guitar Shop Inc</p></footer>
</body>
</html>
code for delete_product.php:
<?php
require_once('database.php');
$product_id= filter_input(INPUT_GET, 'product_id', FILTER_VALIDATE_INT);
$category_id= filter_input(INPUT_GET, 'category_id', FILTER_VALIDATE_INT);
//Delete the product from the datavase
if ($product_id != false && $category_id != false){
$query = 'DELETE FROM products
WHERE productID = :product_id';
$statement = $db -> prepare($query);
$statement -> bindValue(':product_id', $product_id);
$success = $statement->execute();
$statement -> closeCursor();
}
//Display the Product List Page
include('index.php');
?>
Thank you for helping me out! I really appreciate it!
Your are submitting delete button in post method. So you have to receive these value in post method in delete page.
Just change INPUT_GET to INPUT_POST in two lines in delete_product.php
$product_id= filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT);
$category_id= filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT);
instead of
$product_id= filter_input(INPUT_GET, 'product_id', FILTER_VALIDATE_INT);
$category_id= filter_input(INPUT_GET, 'category_id', FILTER_VALIDATE_INT);
You should try with this :
...
$product_id= filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT);
$category_id= filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT);
...

Drag & Drop with PHP & jQuery

I have used the following code found here on a page I am developing and all is working fine.
I would however, like to be able to pull mulitple columns from my database and have them formatted on a table.
I have tried everything and cannot get this to work. Should I be using HTML tables or something else? The code below just displays all columns as one long unformatted row.
<div id="container">
<div id="list">
<ul>
<?php
include("connect.php");
$query = "SELECT id, listorder, description, owner, perc_complete, start_date, end_date FROM acct_project_details WHERE project_id='1' ORDER BY listorder ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$id = stripslashes($row['id']);
$listorder = stripslashes($row['listorder']);
$text = stripslashes($row['description']);
$owner = stripslashes($row['owner']);
$perc_complete = stripslashes($row['perc_complete']);
$start_date = stripslashes($row['start_date']);
$end_date = stripslashes($row['end_date']);
?>
<li id="arrayorder_<?php echo $id ?>">
<?php echo $text; ?>
<?php echo $owner; ?>
<?php echo $perc_complete; ?>
<?php echo $start_date; ?>
<?php echo $end_date; ?>
<div class="clear"></div>
</li>
<?php } ?>
</ul>
</div>
</div>
Many thanks in advance,
John
Use table tr td and TableDnD plugin :
<!-- DOWNLOAD THESE SCRIPTS -->
<script type='text/javascript' src='http://isocra.com/wp-includes/js/jquery/jquery.js?ver=1.10.2'></script>
<script type='text/javascript' src='http://isocra.com/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1'></script>
<script type='text/javascript' src='https://github.com/isocra/TableDnD/blob/master/stable/jquery.tablednd.js'></script>
<script type="text/javascript">
$(document).ready(function() {
$("table.dnd").tableDnD();
});
</script>
<div id="container">
<div id="list">
<table class="dnd">
<?php
include("connect.php");
$query = "SELECT id, listorder, description, owner, perc_complete, start_date, end_date FROM acct_project_details WHERE project_id='1' ORDER BY listorder ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$id = stripslashes($row['id']);
$listorder = stripslashes($row['listorder']);
$text = stripslashes($row['description']);
$owner = stripslashes($row['owner']);
$perc_complete = stripslashes($row['perc_complete']);
$start_date = stripslashes($row['start_date']);
$end_date = stripslashes($row['end_date']);
?>
<tr id="arrayorder_<?php echo $id ?>">
<td><?php echo $text; ?></td>
<td><?php echo $owner; ?></td>
<td><?php echo $perc_complete; ?></td>
<td><?php echo $start_date; ?></td>
<td><?php echo $end_date; ?></td>
</tr>
<?php } ?>
</table>
</div>
</div>

Set a variable in url using php and mysql

I need to set two variable in a url, $id and $job. The data should come from mysql select statement, [id] and [job_number_id]. My page displays a client job proposal and if there is more than one all proposals are displayed but the [id] and the [job_number_id] determines what is displayed on the webpage. I dont have a clue as to how this done. Any help would be greatly appreciated. Here's the code:
<?php
$url = 'http://localhost/estimate_lar/homepage.php';
$id = $_SESSION['id'];
$query = "SELECT id, client_job_name, job_number_id
FROM `job_name`
WHERE `id`='$id'";
$allJobs = $db->query($query);
?>
<?php foreach ($allJobs as $site_title) : ?>
<p>
<tr><?php echo ''.$site_title['client_job_name'],$site_title['job_number_id']. '<br />'.''; ?>
<td></td>
</tr>
</p>
<?php endforeach; ?>
If you want the variables to be avaialble in the URL you need to read them with $_GET.
Getting the arguements from a url such as index.php?id=1&job_number_id=3 will look like that:
if (isset($_GET['id']) && isset($_GET['job_number_id'])) {//make sure both arguments are set
$id = $_GET['id'];
$job_number_id = $_GET['job_number_id'];
}
To set it in your foreach statement:
<?php foreach ($allJobs as $site_title) : ?>
<p>
<tr><?php
$url = "http://localhost/estimate_lar/homepage.php?id=" . $site_title['id'] . "&job_number_id=" . $site_title['job_number_id'];
echo ''.$site_title['client_job_name'],$site_title['job_number_id']. '<br />'.'';
?>
<td></td>
</tr>
</p>
<?php endforeach; ?>
PLEASE remember to read about SQL injection and making sure you are escaping your inputs. Or even better - use a prepared statement.
Currently your script is volunerable, since everyone could just alter the URL and manipluate your DB.
Hope this helps!
Try this.
<?php
session_start();
$id = $_SESSION['id'];
$url = 'http://localhost/estimate_lar/homepage.php';
if($id){
$query = "SELECT id, client_job_name, job_number_id FROM `job_name` WHERE `id`='$id'";
$allJobs = $db->query($query);
}else{
echo "Id not in session";
}
?>
<table>
<?php if ($allJobs) { foreach ($allJobs as $site_title) : ?>
<tr>
<td>
<?php echo $site_title['job_number_id']. " ".$site_title['job_number_id']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php }else{ echo 'No results Found' ;} ?>
This may help you.

displaying data from mysql

Having a little trouble displaying data from a table. Been looking at my code for the past few hours and can't seem to see the problem. What I am trying to do is when a team is clicked on it will go into the players table and display any player that has that team name on the team page. I keep getting a blank page:
index.php
This is the case that launches the team_view.php
case 'view_team':
$team = $_GET['name'];
$teams = get_players_by_team($team);
include('team_view.php');
break;
team_view.php
<?php include '../../view/header.php'; ?>
<?php include '../../view/sidebar_admin.php'; ?>
<div id="content">
<h1>Team Roster</h1>
<!-- display product -->
<?php include '../../view/team.php'; ?>
<!-- display buttons -->
</div>
<?php include '../../view/footer.php'; ?>
team.php
<?php
$team = $team['name'];
$first = $player['first'];
$last = $player['last'];
$age = $player['age'];
$position = $player['position'];
$team = $player['team'];
?>
<table>
<?php foreach ($players as $player) :
?>
<tr>
<td id="product_image_column" >
<img src="images/<?php echo $player['player_id']; ?>_s.png"
alt=" ">
</td>
<td>
<p>
<a href="?action=view_player&player_id=<?php echo
$player['player_id']; ?>">
<?php echo $player['first']; ?>
<?php echo $player['last']; ?>
</a>
</p>
</td>
</tr>
<?php endforeach; ?>
</table>
product_db.php
<?php
function get_players_by_team($team) {
global $db;
$query = 'SELECT * FROM players
WHERE team = :team';
try {
$statement = $db->prepare($query);
$statement->bindValue(':team', $team);
$statement->execute();
$result = $statement->fetch();
$statement->closeCursor();
return $result;
} catch (PDOException $e) {
display_db_error($e->getMessage());
}
}
You never define $players it looks like what you are expecting to be $players is actually $teams.
$teams = get_players_by_team($team);
Additionally youre using $player at the top of the script instead of inside the loop which makes no sense.

links php mysql pagination not working

i'm trying to paginate my results, the limit is working, the number of pages is properly set, but the links of the pagination don't work, i've been looking for a while and nothing, ¿can you take a look and tell me what i'm doing wrong? thanks
<?php
include("config/conexion.php");
$limit = 3;
if(isset($_GET['pag'])){
$pag= $_GET['pag'];
}else{
$pag=1;
}
$offset = ($pag-1) * $limit;
$sql = "SELECT SQL_CALC_FOUND_ROWS id, nombre, local, telefono, celular, email FROM almacenes WHERE id_cat = '".$_GET["id"]."'";
$sqlTotal = "SELECT FOUND_ROWS() as total";
$currentid = $_GET["id"];
$rs = mysql_query($sql);
$rsTotal = mysql_query($sqlTotal);
$rowTotal = mysql_fetch_assoc($rsTotal);
// Total de registros sin limit
$total = $rowTotal["total"];
?>
<?php if($_GET["id"]){ $cat = mysql_query("SELECT * FROM almacenes WHERE id_cat = '".$_GET["id"]."' ORDER BY id ASC LIMIT $offset, $limit"); if(mysql_num_rows($cat)>0){
while($row = mysql_fetch_object($cat)){ ?>
<div class="almacenbox">
<div class="shadow"></div>
<div class="white">
<div class="image"><img src=almacenes/local_111.jpg></div>
<div class="title"><?php echo $row->nombre?></div>
<div class="text">Local: <?php echo $row->local?></div>
<div class="text">Teléfono: <?php echo $row->telefono?></div>
<div class="text">Celular: <?php echo $row->celular?></div>
<div class="text"><?php echo $row->email?></div>
</div>
</div>
<?php } ?>
<?php } }else{ echo "<p>No hay resultados para mostrar</p>"; }?>
<table border="1" bordercolor="#000">
<tfoot>
<tr>
<td colspan="2">
<?php
$totalPag = ceil($total/$limit);
$links = array();
for( $i=1; $i<=$totalPag ; $i++)
{
$links[] = "<a href=almacenes.php?id=$currentid?pag=$i\>$i</a>";
}
echo implode(" - ", $links);
?>
</td>
</tr>
</tfoot> </table>
$links[] = "<a href=almacenes.php?id=$currentid?pag=$i\>$i</a>";
Should be
$links[] = '$i';
Query strings start with a ? but any name-value pairs after that first one require an ampersand.
On a side note you should never place user data directly into your query. This leaves you open to an SQL injection attack. Consider using mysql_real_escape_string or switching to the mysqli library.

Categories