Populating a textarea with database records - php

I populated a textarea with database records using:
<textarea name="textarea" cols="200" rows="20"> <?php
echo "Player Id\t";
while($row = mysql_fetch_array($resourcebuilt)) {
echo stripslashes($row['playerid']);
....;
....;
} ?>
But this isn't exactly 100% what I need. I need to display records in what I believe is a textarea maybe not. But the records need to be clickable so I have functionality to those records (such as edit, delete, or even add a new record to database). Something like what admin panel contains.
I search SO and the web for something similar but with no luck. So does anyone know if this is possible with <textarea> </textarea> or do I need to using something like JavaScript or something related for the interactive functions? If possible provide examples. Thanks you.

I think what you are trying to do is populate a multiline select:
http://www.w3schools.com/tags/att_select_multiple.asp (sorry for w3 schools).
Your code for this would look more like:
<select multiple id="player-id-select">
<?php
while($row = mysql_fetch_array($resourcebuilt)) {
echo '<option>'.stripslashes($row['playerid']).</option>;
....;
....;
}
?>
</select>
Any other interaction (like clicking or whatnot) is done client side via javascript/jQuery:
http://jquery.com/

Related

how can I use php to search a txt or html file and pull out my tables I want to be able to pull everything inside of <table class="class1"> </table>

I have like 6 tables in a page and I want to be able to read that page and pull all the tables
everything in between
I guess I just need to do this 1 time and add a while to do it until the end of the page.
I know how to use the data from a variable later or use it from an array
but I have no clue on how to attempt to grab the data from my webpage
I am going to build a page from that data with a different style but re-using the same tables
Well, using the following requires you to "know" those tables having class="class1" attribute, which you should :)
<?php
$doc = new DOMDocument();
$doc->loadHTMLFile("path/to/filename.html");
$tables = $doc->getElementsByTagName("table");
$validClasses = array("class1", "class2", "class3", "class4", "class5", "class6");
foreach ($tables as $table) {
if(in_array($table->getAttribute("class"), $validClasses)) {
// Here begins your journey :)
}
}
?>
*Now that I understand what you're after - someOne's answer seems a better starting point.
PHP can't 'pull' the data -- javascript can. If you are looking to use the data on the server side with PHP, you will need to send it to that page through POST or GET.
There are a few ways to do that, but perhaps the easiest is to set up a PHP loop on the data page that sets an input inside each field that you wish to send to your php script.
I might do it sorta like this in pseudo code -
echo <form method=post action="phpreader.php">
for ($x = 0 ; $ < $tables; $x++){
echo <table>
echo <tr><td> $data <input type=hidden name='data'.$x value = $data> </td></tr>
echo </table>
}
echo </form>
In the loop, you can put in as many table elements as you need and then attach the data to a form.
This will display your data programatically and also set it up to be send for post at the same time. You will need something to trigger this to go to post. Could be a Submit button, or javascript.
This could work, but I would maybe re-think your program method. If you are displaying this data to begin with, it has to come from somewhere. Might be a better way forward to pull the data from a database, display in the tables, and then you can re-pull the same data using the same query on your phpreader page.

Make variables available inside of a function.(PHP)

Im building an application to track certain information about the clients we deal with like name/date/hrs worked/phone number etc. This information is stored in a db. Because we have different departments like SEO/WEB/Sales etc, and different people within these teams, the app provides different ways to filter the information depending on the filter button pressed.
When someone presses a "filter button", in this example, lets say they pressed the "view by department" button, it takes them to actual hardcoded pages.
As an example:
viewSeoAccs.php
viewWebAccs.php
ViewSalesAccs.php
And in these pages i have queries which pull the information based on the filter pressed but the html is the same. Now here comes the problem.
I have many different pages(based on filters) and every time there is an edit to be made to the html, i have to go into EVERY PHP page to implement the changes.
What i want to do is create a function that spits out the html for me. I have gotten about half way and i know the problem, just cant seem to find a solution.
Here is some code.
In my functions.php file, i have a function called "htmlBlockTEST" that has this code.
EXAMPLE: (code chopped for easy reading)
<?php
function htmlBlockTEST(){
echo '' ?>
<h2 class="accName fl"><?php echo $row['company_name']; ?></h2>
<div class="<?php echo $row['acc_risk']; ?>"> Risk Level. </div>
//ALOT MORE CODE goes here lol.
<?php
}
?>
This is in the header and bought in via "include_once('functions.php').
Under this, i have specific variables that pull in the queried data. (example below)
$pullAllAccounts = "SELECT * FROM tlm_accounts ORDER BY company_name ASC;";
$pullAllAccountsDoIt = mysqli_query($c2d, $pullAllAccounts) or die ("could not pull WEB team data" . mysqli_error($c2d));
?>
now i loop through the db and display the information like so:
<?php
while($row = mysqli_fetch_array($pullAllAccountsDoIt)){
$compName = $row['company_name'];
?>
<?php htmlBlockTEST(); ?>
<?php
}
?>
In this code directly above, where the function call "htmlBlockTEST" is, is where the problem is. Since The variables which hold the queries are outside the function, I'm assuming that they aren't being passed into the function. I dont want to put them inside the function because the HTML is the same throughout all the pages, but not ALL data.
I need the variables that hold for example $row['company_name'] to be also available inside the function so that it doesn't throw "undefined variable" errors.
How can i make this happen? What is the best way to get these variables in the while loop(or otherwise) to be available inside the function???
PS, ive google and found things like $GLOBALS['x'] etc but from what ive read, its not the best way or easiers and overall im confused on how to even use it.
Any help is greatly appreciated.
Thanks in advanced.
Option 1:
while($row = mysqli_fetch_array($pullAllAccountsDoIt)){
htmlBlockTEST($row);
}
Option 2:
global $row;
while($row = mysqli_fetch_array($pullAllAccountsDoIt)){
$compName = $row['company_name'];
htmlBlockTEST();
}
//and in your htmlBlockTEST() function just right this:
function htmlBlockTEST(){
global $row; ?>
<h2 class="accName fl"><?php echo $row['company_name']; ?></h2>
<div class="<?php echo $row['acc_risk']; ?>"> Risk Level. </div>
<?php
//ALOT MORE CODE goes here lol.
}
?>
You have some more options of course.

delete a particular row from session array

I am a beginner in php.
I want to know how a particular row in a php session array can be deleted(not from the database, but from the page only).
I have a table namely, issues having columns issue_id & issue_descrp.
The page is displayed with a table in which each row contains issue and its corresponding id. Each row contain a delete button too. What I want is to delete the corresponding row from the page when I click the button.
This is my php code:
<?php
foreach($_SESSION['meeting_issues'] as $meeting_issues)
{
$query="select issue_id,issue from issues where issue_id='$meeting_issues'";
$result=$_SESSION['connection']->query($query) or die (mysql_error());
while($row = $result->fetch_assoc())
{?>
<?php $issue_id=$row['issue_id']; ?>
<tr><td><?php echo $row['issue_id']; ?></td><td><?php echo $row['issue']; ?></td><td><input type="button" name="<?php echo $row['issue_id']; ?>" id="button" value="Remove"/></td>
</tr>
<?php
}
}
?>
Hope my question is clear. Please help me. Thanks in advance.
use unset to delete array elements such as those in $_SESSION
http://php.net/manual/en/function.unset.php
do not delete the whole session this way, use this instead
http://php.net/manual/en/function.session-unset.php
To remove a row on the page itself, you will need Javascript or jQuery. jQuery is advised because of all the possibilities it gives and it is easier to use than normal Javascript.
jQuery:
$("#button").parents("tr:closest").remove();
Javascript:
document.getElementById('button').parentNode.parentNode.parentNode.removeChild(document.getElementById('button').parentNode.parentNode);
As you can see, jQuery is alot faster and more easy to type.
You are using an ID for the buttons, but the ID is always the same. I recommend using classes for this, because an ID should be unique on a page.
jQuery website

Clearing a selected item from a dropdown using PHP

I used the code below to remove a selected item from drop down, but when I remove one, the other item pops up. For example, if these are my options: "guns, cars, money", as I select and delete guns, cars and money remains. However, if I select cars and delete it, the deleted guns options pops up again. It is frustrating.
<?php
$opts = array("guns","knives","ammo");
$selected = array($_POST['selectMenu']);
$revisedOpts = array_diff($opts,$selected);
?>
<form method="post">
<select name='selectMenu'><?php
foreach($revisedOpts as $v) {
echo "<option>".$v."</option>";
}
?></select>
<input onclick="array_diff()" name="Collect" type="submit" value="submit" />
</form>
PHP only acts when the page is loaded, and you load the same code over and over. In order for previously deleted options to stay deleted, you need some kind of data persistence (like a database). Otherwise, you can use javascript to manipulate the select options on the client side browser. Here is a good discussion
If you must bind the action to onclick() and receive the event on the server side, then you will need to use an AJAX call. The onclick calls a separate PHP script which deletes the option and returns some kind of success message.
you want to have a look at some js code to do this. look at something like that http://www.mredkj.com/tutorials/tutorial_mixed2b.html
use jquery
jquery auto suggestion

Enumerate all Check Box in PHP

I have web page in PHP which displays all records in a table. I want to add check boxes against all rows and user can check a check box to select a row and then submit the page. When the page is submitted I want to enumerate all check boxes and check whether they are checked or not, How can I do this?
You'll create your checkboxes like this:
<input name="rows[]" value="uniqueIdForThisRow" type="checkbox" />
<input name="rows[]" value="anotherId" type="checkbox" />
Then you can loop through them like this:
<?php
// $_POST['rows'] contains the values of all checked checkboxes, like:
// array('uniqueIdForThisRow', 'anotherId', ...)
foreach ($_POST['rows'] as $row) {
if ($row == 'uniqueIdForThisRow') {
// do something
}
}
?>
PHP docs on dealing with forms, see especially Example #3.
Creating the form
You can generate the HTML as follows:
<form [action, method etc]>
<table>
<?php
foreach($dataSet as $dataRow) :
?>
<tr>
<td>
<input type="checkbox" name="dataRow[]" value="<?=$dataRow['id']?>"/>
</td>
[Additional details about datarow here]
<tr>
<?php
endforeach;
?>
</table>
</form>
AFTER POST
look into $_POST['dataRow'] : this will be an array with values the IDS of your $dataRow, so using array_values on $_POST['dataRow'] will give you all the ids of the selected rows:
<?php
$checkedRows = array_values($_POST['dataRow']);
foreach($checkedRows as $row) {
// Do whatever you want to do with the selected row
}
You don’t have to check all checkboxes if they have been checked. Because only successful controls are send to the server. And a checkbox is only successful when it’s checked:
Checkboxes (and radio buttons) are on/off switches that may be toggled by the user. A switch is "on" when the control element's checked attribute is set. When a form is submitted, only "on" checkbox controls can become successful.
So you just have to look what checkboxes you get in the request at all. And if you want to use <select multiple>, take a look at How do I get all the results from a select multiple HTML tag? in the PHP FAQ.
if i were you... i wouldn't fight with altering html table structure.
you can handle that with Javascript frameworks like JQuery which is very effective solution for you. you deal only a few lines of JS code and you don't need exaggerate the html output (i guess it's probably long enough). about jquery there is a good source named visual jquery if you have never used that.
here is the way how to do that.
you dont need to edit inside the loop. you just only put an id to your table tag.
then add new column to your table with checkboxes inside.
then you can get the values of checkboxes & serialize them in to a hidden input. or you can handle selected rows with ajax easy. i think JS framework will work better.
normally i've added many links to post but it says it's not allowed for new users.

Categories