Creating tr value filter - php

I creating filer for website data. I have created value attribute for <tr>which means that the row belongs for specific company. And now i want to make that table would show rows depending which value i will choose. And i really dont have idea how to do that. $currentCompany is that value number of company id. Here is my script:
if($row["type"] != "Staff Comment")
{
print("<tr value=". $currentCompany ." style='height:25px;'>\n");
for ($loop = 0; $loop < count($fields["headings"]); $loop++) {
print("<td bgcolor='white' align=" . $fields["aligns"][$loop] . ">\n");
//translate some information, some not
if ($fields["headings"][$loop] == "Status") {
print($AppUI->_(format_field($row[$fields["columns"][$loop]], $fields["types"][$loop], $row[$fields["columns"][0]])) . "\n");
}
else {
print(format_field($row[$fields["columns"][$loop]], $fields["types"][$loop], $row[$fields["columns"][0]]) . "\n");
}
print("</td>\n");
//print("<tr>" . var_dump($fields) ."</tr>\n");
}
print("</tr>\n");
And here is how it looks like in website:
Hope you can help me guys. For example if i would choose value 25 it would print all tr which has that value.

You want to hide rows, or just not print them? If hide - You have to use JS - PHP cannot modify site dynamically. If just don't print them - use simple if, and possibly You want to pass this information via simple form and POST request.
Create form, which action point to Your script, and have text field where you can specify which company have to be shown. Then, in Your code something like this:
if($currentCompany == $_POST['company'])
{
print("<tr value=". $currentCompany ." style='height:25px;'>\n");
//More code....
}
With JS it will be more complicated - but easy, too. It will have to be done in this way:
Get all TR's
Check every TR value. If value not corresponds to Your needs - add "display: none" CSS attribute.
Concrete JS implementation depends on way you want to code in JS - pure JS, jQuery, other JS framework.

Related

how to load specific files/urls based on a database query

I am trying to alter a piece of code that will allow me to position an image left, right or centred.
I have been able to create radio buttons that populate a table on the database but now I want to pull that information out and create an IF statement that will then open a specified page depending on the result.
For example, If I choose the 'right' radio button, this will then populate the database with the id and the 'right' value. I then want to pull the value from the database so if the value is 'right' the newsitem2.php page will load.
This is the code I have in order to do this:
if ("" . $tnrow['newsimage'] . "" == "none") {
}else {
$radio="SELECT imageposition FROM newsimage";
if ("$radio" == Centre) {
<p class='newsmore'> <a href='newsitem3.php?i=" . $tnrow['niid'] . "'>Read More</a></p>
}
if ("$radio" == Right) {
<p class='newsmore'> <a href='newsitem2.php?i=" . $tnrow['niid'] . "'>Read More</a></p>
}
if ("$radio" == Left) {
<p class='newsmore'> <a href='newsitem.php?i=" . $tnrow['niid'] . "'>Read More</a></p>
}
However, it is not loading each page it just loads the 'newsitem' page with the image on the left. is there any reason for this?
You don't make any call to your database, so $radio is just a String when you use it for now (representing the query) ; you have to send this query to your database (using for example a wrapper like PDO), and then get the result back.
Another thing, are Right, Left, ... constants or enum values ? 'Cause if they're not, you also have to put them into quotes, in order to be interpreted as String
There are a lot of weird instructions in your code, you should probably get more informations on PHP, 'cause you doesn't really seem to understand the concepts for now. Get a good tutorial/book, and you'll see that what you want to do is pretty easy when you got the ideas behind. ;)
Your IF's are all wrong. I assume the value of $radio is a string?
if ("$radio" == Centre) {
Should be
if ($radio == 'Centre') {
And so on for all the others

Use jQuery and Javascript to alter inputs in dynamically created PHP table

I am trying to build a table using PHP and SQL values. One of those table columns is going to have a numerical input that controls other values in that row. This parameter ranges from 0 to 1 and I'm trying to use Javascript to dynamically update the three columns to the right of it depending on the parameter value.
Here is the PHP commands for the html table:
$i = 1;
while($row = mysql_fetch_array($result)){
Print "<tr>";
Print "<td><input id='toChange[$i]' name='toChange[$i]' type='number' step='0.01'
min='0.01' max='0.99' value='.85' </td>";
Print "<td>".$row['dependentOnParameter1'] . "</td>";
Print "<td>".$row['dependentOnParameter2'] . "</td></tr>";
$i++;
}
I have tried many different tags for the input cell, such as toChange[] and toChange$i (this one as if the number grows with the row number). This table prints correctly, and here is the jQuery function I have tried to implement:
$('#prEff[1]').change(function() {
var test = this;
return;
});
I know how to alter the table once I get this function to correctly call, but the use of an input inside an array seems to be causing trouble. Is there a specific jQuery object or tag that must be used for .change for arrays? Or if I can use the table tag, will the this pointer point to the part of the table that has been altered?
You probably need to escape the brackets if they are part of the selector:
$('#prEff\\[1\\]')

How do i pass a mysql query result as a variable to be used in another query search?

Ok so basically I've got a product database. members search the database by clicking on categories of their choice.
These categories are spans which all trigger a javascript (ajax) function which calls a php query to find all of the subcategories of the span just clicked.
Those subcategories are outputted in a while loop which creates more spans which also trigger the same js function which will link back to the query.
The code below shows the output of the categories displayed based on the output of the database query.
"<div id=\"div1\">";
while($row = mysql_fetch_array($result))
{
$spans++;
echo " <span id=\"$spans\"
onclick='serverconnect(\"http://localhost/dreamweaver/Website/webdev/Code structure.php\",
\"nya\")'> $row[1] </span>";
echo "<br />";
}
"</div>";
So let's say the output of this is:
HEALTH
FITNESS
EDUCATION
LEISURE
How could i pass the span names as values for the next query so that whatever span is clicked on gets its appropriate value passed to the database search?
Would i have to somehow assign the contents of each span to a variable and then pass the variable to the javascript function which would then pass it to the query?
Im out of ideas as to how to do that, or if i should do it another way.
Thanks!
Try replacing:
echo " <span id=\"$spans\"
onclick='serverconnect(\"http://localhost/dreamweaver/Website/webdev/Code structure.php
\", \"nya\")'> $row[1] </span>";
With:
echo " <span id=\"$spans\"
onclick='serverconnect(\"http://localhost/dreamweaver/Website/webdev/Code
structure.php?value=" . urlencode($row[1]) . "\", \"nya\")'> $row[1] </span>";
You may then retrieve the value in $_GET['value'] after clicking the SPAN.
Note: I added some line breaks in the text for ease of reading, don't forget to remove them.

Single dynamic values submitted through one form

I am using Jquery UI Selectable. The user has the option to dynamically add new list items to the original setup.
I would like to include a 'clear' button that will give the user the ability to clear each individual item they created by clicking on an X input submit (img).
HTML (php)
if ($this->session->userdata('inactivefilter') == true) {
$inactivefilter = $this->session->userdata('inactivefilter');
$i=0;
foreach ($inactivefilter as $filter)
{
$filterdash = implode('-', explode(' ', $filter));
echo "<li class='ui-state-default' id='$filterdash'>$filter</li>";
echo "<div id=clear>
<form method='POST' action='".base_url()."main/clear_filter'>
<input type='image' name='filtervalue' value='$i' src='".base_url()."img/board/icons/clear.png'></input>
</form>
</div>";
$i++;
}
}
This is where the list is created. specifically the clear_filter action form.
Clear_filter currently 'attempts' to grab the value of '$i' but I don't know how to pass that correctly.
here is the controller:
public function clear_filter()
{
$i = $_POST['filtervalue'];
$this->thread_model->clear_filter($i);
}
I'll omit the clear_filter model due to its irrelevance to the problem.
Essentially, I just need $i to be picked up based on whatever value is clicked on in the dynamic form on the actual page.
Any help would be appreciated!
Well, it seems like I just had things a bit backwards.
The code was more or less correct.
For Codeigniter, you catch the passed input value=$i by using the name ="filtervalue"
Change the controller code to :
$i = $this->input->post('filtervalue');
and $i is set to whatever value was clicked on.

Deleting Database Records With a checkbox?

I just finished my news posting system and I need help to make the news deletion system.
Right now I have this code to load the each post ID and TITLE into a table as well as a checkbox beside it.
<?php
$news = mysql_query('SELECT `id`,`title` FROM news');
if (!$news)
{
exit('<p>Error retrieving news from database!<br />'.
'Error: '.mysql_error().'</p>');
}
while ($new = mysql_fetch_array($news))
{
$id = $new['id'];
$title = $new['title'];
echo "<tr style='background: #3D3D3d; width: 400px; font-family: Century Gothic; font-size: 15px;'>";
echo "<td style='padding-left: 5px'><input type='checkbox' name='id' value='$id' /></td> <td style='padding-left: 15px'>$id</td> <td style='padding-left: 15px'>$title</td>";
echo "</tr>";
}
?>
I want to know how to delete each database record by id using checkboxes.
First i would recommend an Archive system over Deleting just so that the data is stored for any reason you would have to call on it, no matter how little importance the entry has.
an idea would be to break up the code so that it makes it easy to read:
All styles into a CSS file or CSS script takes and use classes and
IDs to set up style (its a bit hard to read your code)
Have SQL procedures call to manage the SQL data to make your code
able to change from PHP to something else if you ever need to
to add my idea to the code
<?php //deletenews.php
//....
$result = $mysql_query("Call Delete_news_Entry($id)");
if (!$result)
{
die("Error deleting record from news" . mysql_error);
}
//.... end of php
?>
SQL Procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS $$
CREATE PROCEDURE DB.Delete_news_entry(in_ID INTEGER)
BEGIN
DELETE FROM news WHERE id=in_ID;
END $$
DELIMITER ;
(if you want it to be live over submit button)
javascript
<Script type='text/javascript'>
//depends on jquery
function deletenews(id)
{
var spanid = "#span"+id;
$.get("deletenews.php",
null,
function(){
Alert("Complete") //this is more or less for you to put your required code here
$(spanid).hide();
},
"html")
.error(function(){
Alert("error") // for your own code to go here
});
}
</script>
sorry, just modifying your code
//in your php code, for the event, try onclick or onchange to trigger the delete
echo "<span id='span$id'><td class='left'><input type='checkbox' name='id' value='$id' onclick='deletenews($id)' /></td><td class='left'>$id</td> <td class='left'>$title</td></span>";
Having everything a part makes it easy to read.
i have been practicing breaking my code up for a long time now, i even draw maps of how all my files relate instead of having a page of php 1000 lines of code or more.
Right now it seems that you're only creating the table rows, but I'm gonna go ahead and assume these are within in the table element. If this table is also within a form element, you can delete all the records with a checked checkbox on submit.
You need to use the PHP isset function in the form's action reference to check if the checkbox is set and a foreach loop to run through them all. Something like this:
<?php
foreach($_POST as $key =>$value){
if(isset($_POST[$key])){
$value = mysql_real_escape_string($value);
mysql_query("DELETE FROM table WHERE id = $value");
}
}
?>

Categories