Filters from database in PHP - php

echo 'Qname : <select id ="selector">';
$s = mysql_query('SELECT qid,qname FROM q_table ORDER BY qname');
while($row = mysql_fetch_array($s))
{
$filter = $row['qname'];
$filterid = $row['qid'];
echo '<option value='.$filterid.'>'.$filter.'</option>';
echo '</select>';
echo '<div id='.$filterid.' class="colors" style="display:none;">';
echo '<table>
<tr><th>USERID</th>
<th>QNAME</th>
</tr>';
$publishedqnames = mysql_query('SELECT qid, qname, quserid FROM pub_q');
while($row = mysql_fetch_array($publishedqnames))
{
$qid = $row['qid'];
$qname = $row['qname'];
$quserid = $row['quserid'];
echo '<tr>
<td>'.$quserid.'</td>
<td>'.qname.'</td>
</tr>';
}
echo '</table></div>';
}
when I select qname, the div of that selected filter(filter and qname are same) needs to get displayed
JQUERY FILE
$(function()
{
$('#selector').change(function(){
$('.colors').hide();
$('#' + $(this).val()).show();
});
});
Rightnow Iamn't able to display the correct qid with correct div

Try this
echo 'Qname : <select id ="selector">';
$s = mysql_query('SELECT qid,qname FROM q_table ORDER BY qname');
while($row = mysql_fetch_array($s))
{
$filter = $row['qname'];
$filterid = $row['qid'];
echo '<option value='.$filterid.'>'.$filter.'</option>';
}
echo '</select>';
?>
<script>
$(function()
{
$('#selector').change(function(){
$('.colors').hide();
$('#' + $(this).val()).show();
});
});
</script>
<?php
echo '<table>
<tr><th>USERID</th>
<th>QNAME</th>
</tr>';
$publishedqnames = mysql_query('SELECT qid, qname, quserid FROM pub_q');
while($row = mysql_fetch_array($publishedqnames))
{
$qid = $row['qid'];
$qname = $row['qname'];
$quserid = $row['quserid'];
echo '<div id='.$filterid.' class="colors" style="display:none;">';
echo '<tr>
<td>'.$quserid.'</td>
<td>'.qname.'</td>
</tr>';
echo '</div>';
}
echo '</table>';
please let me know if not working

Related

jQuery load() not displaying echo table from php | wordpress

I'm using Wordpress and I want to display a table, which contains pictures and buttons. This table should get loaded with a jquery load() method and put the result into a div.
I get the result, which I want, but it doesn't display in the div. Just here:
here.
When I just put, for example, a H2 with text and without any php into the php file, then it gets displayed.
Script
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
function GetURLParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
jQuery(document).ready(function(){
var $id = GetURLParameter('id');
var urlGetItem = "getItems.php/?id=" + $id;
jQuery('#load_surveys').load(urlGetItem);
});
</script>
HTML
<div id="load_surveys"></div>
PHP
<?php
include_once 'db.php';
require ('../wp-blog-header.php');
global $wpdb;
global $current_user;
get_currentuserinfo();
$userId = $current_user->ID;
$id = $_GET['id'];
$result = mysqli_query($conn,"SELECT * FROM f5_users JOIN COMPARISONFOLDER ON COMPARISONFOLDER.USER_ID = f5_users.ID JOIN ITEM ON ITEM.COMPARISONFOLDER_ID LIKE COMPARISONFOLDER.COMPARISONFOLDER_ID WHERE COMPARISONFOLDER.COMPARISONFOLDER_ID LIKE $id AND f5_users.ID LIKE $userId");
echo "<table border='1'>
<tr>
<th>Item</th>
<th>Delete</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo '<td><img src="data:image/jpeg;base64,'.base64_encode($row['ITEM']).'" width="500" height="auto"/></td>';
echo "<td><button type='button' onclick='deleteItem(".$row['ITEM_ID'].", $id)'>Testing Script</button></td>";
echo "</tr>";
}
echo "</table>";
?>
What do you get if you make console.log('id',$id) and console.log('url,urlGetItem)
jQuery(document).ready(function(){
var $id = GetURLParameter('id');
console.log('id',$id);
var urlGetItem = "getItems.php/?id=" + $id;
console.log('url,urlGetItem);
jQuery('#load_surveys').load(urlGetItem);
});
PHP
$markup = "<table border='1'>
<tr>
<th>Item</th>
<th>Delete</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$markup .= "<tr>";
$markup .='<td><img src="data:image/jpeg;base64,'.base64_encode($row['ITEM']).'" width="500" height="auto"/></td>';
e "<td><button type='button' onclick='deleteItem(".$row['ITEM_ID'].", $id)'>Testing Script</button></td>";
$markup .= "</tr>";
}
$markup .= "</table>";
echo $markup;

How do select one element in a while loop (Re-assign a variable inside a while loop)

The program "loops and returns" all the entries in in a folder on the server.
but my problem is: When the user selects any of the returned entries, only the first element is returned.
How do I return the user selected entry?
p.s I'm a noob at php
modal refers to "bootstrap modal"
<?php
include_once("include/main.inc.php");
$query ="SELECT job_id, job_logo, job_company, job_title, job_area, job_county FROM job_tbl";
$result = mysql_query($query) or die (mysql_error());
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($row = mysql_fetch_array($result)){
$id = $row['job_id'];
$d = $row['job_logo'];
echo '<div class="col-xs-6 col-md-6">';
echo '<a href ="#" ><h6>'.$row['job_title'] .'</h6> </a>';
echo 'View';
echo '</div>';
}
$memid = $row['member_id'];
$query2 ="SELECT job_id, member_id, job_logo, job_category, job_description, job_skills, emp_type, job_experiance, job_vacancy, job_salary, expery_date, job_contact_name FROM job_tbl";
$modalResult = mysql_query($query2) or die (mysql_error());
if (!$modalResult) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($row = mysql_fetch_array($modalResult)){
$d = $row['job_logo'];
echo '<div class = "modal fade" id= "view" role ="dialog">';
echo '<div class ="modal-dialog">';
echo '<div class = "modal-content">';
echo '<div class ="modal-header">';
echo '<h4>Contact Locate.ie</h4>';
echo '</div>';
echo '<div class= "modal-body">';
echo '<div class ="container">';
echo '<div class="jumbotron">';
echo '<legend class= "scheduler-border">';
echo '<h5>Description:</h5>';
echo '<h6>'.$row['job_description'].'</h6>';
echo '</legend>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div></div></div>';
}
?>
You wrote :
while($query2 = mysql_fetch_array($modalResult))
{
$d = $row['job_logo'];
echo "<img src=\"http://www.foo.ie/images/job/".$d."\" >";
}
It's probably a copy/paste typo. Shouldn't it be :
while($query2 = mysql_fetch_array($modalResult))
{
$d = $query2['job_logo'];
echo "<img src=\"http://www.foo.ie/images/job/".$d."\" >";
}
Also, as adviced before, try to learn PDO/prepared statements

Set approval for admin with checkbox using php,jquery and ajax

I am working in a project where i want to show only those products to user which are selected by admin from database. Actualy i want to set the approval 1 or 0 in database when admin check or unchecked that checkbox.
jobs.php
$(document).ready(function(){
$('input.check').click(function(){
if($("input.check").is(':checked'))
{
$id = $(this).attr("id");
$.post("handle.php",{action:"checked",id:$id},function(data){
alert("Peoduct is set to display...");
});
}
else
{
alert("unchecked");
$id = $(this).attr("id");
$.post("handle.php",{action:"unchecked",id:$id},function(data){
alert("Peoduct is un-set to display...");
});
}
});
});
<?php
$dbqry = mysql_query("select * from job_category");
echo "<table width='50%', border='2'>
<tr>
<th>Catergory ID</th>
<th>Category Name</th>
<th>Remove</th>
<th>Update</th>
<th>Approval</th>
</tr>";
if($dbqry)
{
while($row = mysql_fetch_array($dbqry))
{
echo "<tr>";
echo "<td align='center'>" . $row['c_id'] . "</td>";
echo "<td align='center'>" . $row['cat_name'] ."</td>";
$cid = $row['c_id'];
$aprv = $row['approval'];
echo "<td align='center'><a href='remove.php?action=cat_remove&cid=$cid'>Remove</a></td>";
echo "<td align='center'>
<a href='Update-form.php?action=cat_update&cid=$cid'>Update</a></td>";
?>
<td align="center">
<input type='checkbox' name='approval' value='approval' id ="<? echo $cid; ?>" class="check"/>
</td>
</tr>
<?
}
echo "</table>";
echo '<br/>';
echo "<a href='add.php?action=cat_add'>Add New Category</a>";
}
else
{
die(mysql_error());
}
?>`
handle.php
`<?php
include 'include/connection.php';
$action = $_POST['action'];
$id = $_POST['id'];
//echo $action;
if($action == "checked")
{
$query = "update job_category set approval=1 where c_id=$id";
$result = mysql_query($query);
if(!$result)
{
echo die(mysql_error());
}
}
else if($action == "unchecked")
{
$query = "update job_category set approval=0 where c_id=$id";
$result = mysql_query($query);
if(!$result)
{
echo die(mysql_error());
}
}
?>`
Its working but when i refresh the page or seletc the URL and press enter then all the checked data appears unchecked even after that it does not change value of approval from 1 to 0 in database, but still it make me confuse about which items are checked or unchecked. Any suggestion will be appreciated.
modify checkbox line to show checked if approval=1 in database... try this
<input type='checkbox' name='approval' value='approval' <?php if($row["approval"]==1){ echo "checked=\"checked\"";}) ?> id ="<? echo $cid; ?>" class="check"/>
html
<input type='checkbox' name='approval' value='approval' <?php echo $row["approval"]?"checked=\"checked\"":'';?> id ="<? echo $cid; ?>" class="check"/>
then add js
$(function(){
$('input[type=checkbox]').each(function(){
if($(this).attr('checked') && $(this).attr('checked')=='checked'){
$(this).attr('checked', true);
}else{
$(this).attr('checked', false);
}
})
})
and
if($("input.check").is(':checked'))
to
if($(this).is(':checked'))

show my result in a selected div

I have this form:
<FORM id="frmVote" action="../vote_dir/proccess.php" method="post">
<table id="tblMain" align="center">
<tr>
<td class="header"></td>
</tr>
<tr>
<td>
<?php
include "../vote_dir/loadpoll.php";
?>
</td>
</tr>
<tr>
<td>
<input id="votefor" name="votefor" type="hidden"/>
</td>
</tr>
<tr>
<td class="button">
<INPUT class="btnVote" onclick="return confirmSubmit()" type="submit" value="vote"/>
</td>
</tr>
<tr>
<td class="footer"></td>
</tr>
</table>
</FORM>
and this is part of the proccess.php:
<?php
$votefor = $_POST["votefor"];
//Get the IP of the user
$domain = $_SERVER["REMOTE_ADDR"];
$today = date("m/d/Y");
echo "<table id=\"tblResults\" align=\"center\">";
//MORE CODE
// Generate the results table
$doc = new DOMDocument();
$doc->load("../vote_dir/xml/results.xml");
$pollitems = $doc->getElementsByTagName("pollitem");
foreach( $pollitems as $pollitem )
{
$entries = $pollitem->getElementsByTagName("entryname");
$entry = $entries->item(0)->nodeValue;
$votes = $pollitem->getElementsByTagName("votes");
$vote = $votes->item(0)->nodeValue;
$tempWidth = $vote / $maxvotes;
$tempWidth = 300 * $tempWidth;
$votepct = round(($vote / $maxvotes) * 100);
echo "<tr><td width=\"45%\" class=\"polls\">$entry</td>";
echo "<td width=\"35%\" class=\"resultbar\"><div class=\"bar\" style=\"background-color: ";
getRandomColor();
echo "; width: $tempWidth px;\">$votepct%</div></td><td class=\"each_vote\" width=\"20%\">($vote votes)</td></tr>";
}
echo "<tr><td width=\"45%\" class=\"total\" colspan=\"3\">Σύνολο ψήφων: $maxvotes</td>";
echo "</table>";
?>
Like it is now it works fine. but I am trying to make it load my results in a specific div instead of opening the results like
I am trying to use this jquery (that I used in other things), it is working partialy since it opens the results in the div but it seems it doesn't sent the form into the proccess.php since I can see the result page without any change or message e.g "You have already voted".
.delegate('.btnVote', 'click', function(){
var keyValues = {
votefor : $(this).parent().find('input[name="votefor"]').val()
};
$.post('../vote_dir/proccess.php', keyValues, function(rsp){
$('#voting').load('../vote_dir/results.php');
});
return false;
})
this is my loadpoll.php
<?php
// Load the results xml file
$doc = new DOMDocument();
$doc->load("../vote_dir/xml/results.xml");
$root = $doc->getElementsByTagName("results")->item(0);
$question = $root->getAttribute("question");
echo "<table id=\"tblPoll\" align=\"center\"><tr><td class=\"question\">$question</td></tr>";
echo "<tr><td class=\"pollitem\">";
$pollitems = $doc->getElementsByTagName("pollitem");
$id = 1;
// Loop through each item, and create a radio button for each item
foreach( $pollitems as $pollitem )
{
$entries = $pollitem->getElementsByTagName("entryname");
$entry = $entries->item(0)->nodeValue;
$votes = $pollitem->getElementsByTagName("votes");
$vote = $votes->item(0)->nodeValue;
if ($id==1)
echo "<input id=\"entry$id\" class=\"radiobutton\" onclick=\"setVote('$entry')\" type=\"radio\" name=\"poll\" value=\"$entry\">$entry<br>";
else
echo "<input id=\"entry$id\" onclick=\"setVote('$entry')\" type=\"radio\" name=\"poll\" value=\"$entry\">$entry<br>";
$id = $id + 1;
}
echo "</td></tr>";
echo "</table>";
?>
$('.btnVote').live('click', function () {
$.post('../vote_dir/savevote.php', { votefor: $.trim($(this).parents('form').find('input[name="votefor"]').val()) }, function (data) {
$('#voting').html(data);
//Or
//$('#voting').load('../vote_dir/results.php');
});
return false;
});
Try changing the
$(this).parent().find('input[name="votefor"]').val()
to
$('#votefor').val()
I think the "$(this).parent()" leads to the <td>
Try this:
var data = $(this).parent().find('input[name= votefor]').val();
or
$data = $(this).parent().find('input[name= votefor]').val();

Show all rows in mysql table then give option to delete specific ones

I want to have the ability to show all the entries in a database table and by each one give the user the ability to delete specific ones.
I am currently using a for each loop that loops through the database showcasing each entry.
$result = mysql_query("SELECT * FROM KeepScores");
$fields_num = mysql_num_fields($result);
echo "<table><tr>";
// printing table headers
echo "<td>Recent Posts</td>";
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
How would to add a delete button that appears by each one and removes the entry from the database table?
You can do it with forms:
//main.php
<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>
<table>
<tr>
<td>Recent Posts</td>
</tr>
<?php while($row = mysql_fetch_array($result)) : ?>
<tr>
<td><?php echo $row['field1']; ?></td>
<td><?php echo $row['field2']; ?></td>
<!-- and so on -->
<td>
<form action="delete.php" method="post">
<input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<?php endwhile; ?>
</table>
//delete.php:
<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
header('Location: main.php');
}
Or you can do it with jQuery and AJAX:
//main.php
<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>
<table>
<tr>
<td>Recent Posts</td>
</tr>
<?php while($row = mysql_fetch_array($result)) : ?>
<tr id="<?php echo $row['id']; ?>">
<td><?php echo $row['field1']; ?></td>
<td><?php echo $row['field2']; ?></td>
<!-- and so on -->
<td>
<button class="del_btn" rel="<?php echo $row['id']; ?>">Delete</button>
</td>
</tr>
<?php endwhile; ?>
</table>
<script>
$(document).ready(function(){
$('.del_btn').click(function(){
var del_id = $(this).attr('rel');
$.post('delete.php', {delete_id:del_id}, function(data) {
if(data == 'true') {
$('#'+del_id).remove();
} else {
alert('Could not delete!');
}
});
});
});
</script>
//delete.php
<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$result = mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
if($result !== false) {
echo 'true';
}
}
It's all untested and sure needs some adjustment for your specific project, but I think you get the idea and I hope it helps.
Next time, please post your schema if you ask stuff about database.
I thought I would improve on this a little bit by wrapping the delete post in a class and function. I was having the same problem. and this worked great for me. Thanks again # Quasdunk
<?php
// Class to hold the remove post function
class someClass{
//Function for removing the post
function removePost(){
if(isset($_POST['delete_id']) && (!empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$result = mysql_query("DELETE FROM post WHERE post_id='".$delete_id."' AND post_member='" . $_SESSION['SESS_USER'] . "'");
if($result !== false) {
echo 'true';
}
}
}
}
if(isset($_SESSION['SESS_MEMBER_ID'])){
$member = $_SESSION['SESS_USER'];
$res = mysql_query("SELECT * FROM post WHERE post_member='$member' ORDER BY timestamp DESC") or die(mysql_error());
$i = new someClass;
while($row = mysql_fetch_array($res)){
echo '<div style="width:100%;margin:0 auto;border-top:thin solid #000;">';
echo '<div style="width:600px;margin:0 auto;padding:20px;">';
echo $row['post_text'] . '<br>';
$postID = $row['post_id'];
echo '<div style="border-top:thin solid #000;padding:10px;margin-top:5px;background-color:#CCC;">';
echo 'You posted this on: ' . $row['post_date'] . '#' . $row['post_time'];
echo '<div style="float:right;">
<form method="post" action="'. $i->removePost() .'">
<input type="hidden" name="delete_id" value="'.$row['post_id'].'" >
<input type="submit" value="Delete Post">
</form>
</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
?>
Produce a key to each table, using jquery,then link it to a php file which an accept the key and delete from the specific table (which also can be passed through jquery)

Categories