php Variable name must change in for loop - php

On my form I've got a select from a table. I've put it in a for loop so that each time the name of the select changes to option0, option1, etc. Then on the action page of the form I need to get the info for each option selected, eg $_POST['option0'], $_POST['option1'], etc to input it into a table. I can't get the action page to work, what am I doing wrong?
PAGE1
for ($x = 0; $x <= 14; $x++) {
$get2a = mysqli_query($con,"SELECT * FROM table");
$opt = "<select name='interviewer". $x . "'>";
while($row2a = mysqli_fetch_array($get2a)) {
$intvID = $row2a['intvID'];
$opt .= "<option value = '";
$opt .= $row2a['intvID'];
$opt .= "'>";
$opt .= $row2a['intvID'];
$opt .= "</option>";
}
PAGE 2
for ($y = 0; $y <= 14; $y++) {
echo $_POST['interviewer . $y . '];
}

You are calling your selectable options sets $opt = "<select name='option". $x . "'>";
So Page2 should be looking for things names 'option0' ... 'option13'` like this
for ($y = 0; $y <= 14; $y++) {
echo $_POST['option' . $y]; <-- notice slight syntax change also
}

Have you tried printing the post array? Use print_r:
print_r($_POST);
I don't believe the post has something that is named 'interviewer .... something.
besides, if you wish to mix text and variables, use
$_POST["interviewer {$x}"]
remove the single quotes and the dots. Single quotes do not evaluate variables:
$_POST['interviewer . $y . ']; -> wrong

Related

How do I create a button or hyperlink for every row of a PHP/HTML web page that displays rows of a SQL table?

I am using Linux, Apache, PHP and Postgres for my web application program. I have a web application that returns the content of SQL tables. The user has to log in and type in a valid SQL table name. How can I create a button for each row that is returned and displayed in the web UI? It has to be dynamic because the rows returned vary depending on the table the user enters.
Ultimately I'd like to have the user update the row in the SQL database through the web UI. For now I want a button to appear for each row that is returned. If the button or link was available for every cell (regardless of whether the cell was empty or not), that would work. The rows returned are currently in static alphanumeric text. I'd like there to be a hyperlink or button for each row. I can then have the user go to a new .php web page from that link or button. This page will alow for an update to the SQL table.
How do I create a button or hyperlink in a web page for each SQL row returned and displayed in a PHP web page?
The code below leaves out the authentication web page. Here is my code for the web page that is repeatedly called as it allows users to type in a table name and view it:
<?php
session_start();
$pa = $_POST["table"];
$host = $_SESSION['hostv'];
$port = $_SESSION['portv'];
$dbname = $_SESSION['dbv'];
$credentials = $_SESSION['cv'];
$comp = $credentials;
$db = pg_connect( "$host $port $dbname $credentials" );
$query = 'select * from ' . $pa . ';';
$cc = pg_query($query);
showingfunc($cc);
function showingfunc($cc)
{
/* This code below was copied and modified from razorsql.com */
$i = 0;
echo '<html><body><table><tr>';
if ($cc) {
}
else {
echo '<td>' . "Failure." . $dbname . '</td>';
}
while ($i < pg_num_fields($cc))
{
$fieldName = pg_field_name($cc, $i);
echo '<td>' . $fieldName . '</td>';
$i = $i + 1;
}
echo '</tr>';
$i = 0;
while ($row = pg_fetch_row($cc))
{
echo '<tr>';
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo '</tr>';
$i = $i + 1;
}
pg_free_result($cc);
echo '</table></body></html>';
}
?>
<html>
<body>
<form action="repeater.php" method="post">
TableToView: <input type="text" name="table"<br>
<input type="submit">
</form>
Logout
</body>
</html>
Simply add a cell containing your action button.
while ($row = pg_fetch_row($cc))
{
echo '<tr>';
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
//New Code on the line below
echo '<td><a class="edit" href="[YOURLINK]">Edit</a></td>' //You Probably want to add an ID to your query string here
echo '</tr>';
$i = $i + 1;
}
Also, add an empty cell in your header in a similar fashion.
you just need to add one more td tag in your code...
we just passed id of your record with href of a tag.
we just replace your code with easy way
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
replace with :-
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
echo '<td>Update</td>';
next($row);
$y = $y + 1;
}
and you will get this id on another page by doing:-
$id=$_REQUEST['cid'];
and pass it to your query so you can get all information which this will have in table.
Please let me know if any concern or if not working... Thank You

Distinguish between elements and last element of array

Im creating tablerows based on the number of the array colours:
$query = mysql_query("SELECT * FROM things);
$num = mysql_num_rows($query );
$colours = array ();
if($num)
{
for ($i = 0; ($row = mysql_fetch_assoc($query)); ++$i)
{
$colours[$i] = $row["colours"];
}
}
$arrlength = count($colours);
for ($i = 0; ($i < ($arrlength)); ++$i){
echo "
<tr class='border_bottom'><td>".$colours[$i]."</td></tr>
";
}
So, if colours is, lets say, equal to 8, 8 table rows with the class border_bottom are created.
border_bottom is used by CSS to add a border to the bottom of each tablerow.
What I need is some PHP help: I need code which checks the array colours. The last element of the array has to go with an empty id since I dont want a border-bottom added to that very last tablerow. All other tablerows have to go with the border_bottom class, tho.
I was thinking of starting the code like that:
echo"
<tr class='
";
-->PHP code goes here<--
echo"
'>
<td>".$colours[$i]."</td></tr>
Try this:
<?php
$query = mysql_query("SELECT * FROM things");
$num = mysql_num_rows($query);
$colours = array();
if($num)
{
while($row = mysql_fetch_assoc($query))
{
$colours[] = $row["colours"];
}
}
$arrlength = count($colours);
for ($i = 0; $i < $arrlength; ++$i){
if($i < $arrlength - 1){
echo "<tr class='border_bottom'><td>{$colours[$i]}</td></tr>";
}else{
echo "<tr><td>{$someColor}</td></tr>";
}
}
Try the following code in your table row echo
echo "<tr"
.($i < $arrlength - 1 ? " class='border_bottom'" : "")
.">"
."<td>{$colours[$i]}</td></tr>";
You can actually do this while fetching the rows without needing to count how many there are, by reading ahead one row.
$previous_row = mysql_fetch_array(); // fetch the first row (if there is one)
while ($previous_row) {
if ($row = mysql_fetch_array()) {
// if another row is fetched, then the previous row was NOT the last row
echo '<tr class="border_bottom"><td>' . $previous_row['colours'] . '</td></tr>';
} else {
// otherwise, the previous row WAS the last row, and does not get the class
echo '<tr><td>' . $previous_row['colours'] . '</td></tr>';
}
$previous_row = $row; // Set the previous row to the current row
}

how can I higlight the correct cell in PHP

I am trying to make a function in PHP which writes out a table, and looks in the database to find what cells should have info. the grid will always be the same size, but the content may be in different places.
I've gotten it to be able to look in the database, though it seems to only highlight the first cell, rather than the correct coordinates.
require("sql.php");
$sql = <<<SQL
SELECT *
FROM `maps`
WHERE `objpresent` = 1
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
} // ran the query
$xobj = array();
$yobj = array();
while($row = $result->fetch_assoc()){
//echo $row['x'] . $row['y'] . $row['object'] . '<br />';
$xobj[] += $row['x'];
$yobj[] += $row['y'];
}// get the rows
//find whether the row is obstructed
for($a=0; $a<=20-1; $a++) //rows (y)
{
for($i=0; $i<=25-1; $i++) //cols (x)
{
echo "<td>"; //between these write the apt content
// if (empty($xobj[$i]) || empty($yobj[$a]) ){
// echo '0';
//} //detect whether there is even a record for this space
if(!empty($xobj[$i]))
{
if(!empty($yobj[$a]))
{
echo $xobj[$i]; //debug
if($xobj[$i] == $i)
{
//echo $xobj[$i];
echo "A";
}
}
}
//echo "<td><img src='emptysym.png'></img></td>";
echo "</td>"; //add textual descriptions for now, add icons later
}
echo "</tr>";
}
this is my current(though rather messy) code.
if there is a row with the column x saying 2, and the column y saying 3, then it should put a letter at (2,3.
is it possible to fix this, or is there a better method for this?
Use a 2-dimensional array whose indexes are the x and y values from the database:
$xyobj = array();
while($row = $result->fetch_assoc()){
$xyobj[$row['x']][$row['y']] = true;
}
Then your output loop should be:
for ($y = 0; $y < 20; $y++) {
echo '<tr>';
for ($x = 0; $x < 25; $x++) {
echo '<td>';
if (isset($xyobj[$x][$y])) {
echo 'A';
}
echo '</td>';
}
echo '</tr>';
}

How to exclude MySQL query from my loop (limit unnecessary queries)

I'm asking MySQL for data, but it slows down the whole script. Yet I have no idea how to get this out of a loop. I tried converting it to PHP array but honestly after day of tries I failed.
<?php
$id = '1';
include_once 'include_once/connect.php';
for ($x = 1; $x <= 5; $x++) {
for ($y = 1; $y <= 5; $y++) {
$xy = $x."x".$y;
$pullMapInfo = "SELECT value FROM mapinfo WHERE id='".$id."' AND xy='".$xy."'";
$pullMapInfo2 = mysql_query($pullMapInfo) or die('error here');
if ($pullMapInfo3 = mysql_fetch_array($pullMapInfo2)) {
#some code
} else {
#some code
}
}
}
?>
How to get MySQL query $pullMapInfo2 out of loop to shorten loading it by asking once?
If you want to fire script on your localhost you can c&p whole thing :-)
I'm not sure what you have in your table, but considering you are basically looping through virtually everything in it, I'd say do a single query for the given Id and then sort out what you need from the larger dataset.
Especially if you are always pulling back essentially the complete dataset for each id, there's no reason to even bother with the IN query, just pull it all back into a single PHP array, and then iterate through that as needed.
Use a MySQL IN clause
<?php
$id = '1';
include_once 'include_once/connect.php';
// first we create an array with all xy
$array = array();
for ($x = 1; $x <= 5; $x++) {
for ($y = 1; $y <= 5; $y++) {
$xy = $x."x".$y;
$array[] = $xy;
}
}
$in = "'" . implode("', '", $array) . "'";
$pullMapInfo = "SELECT xy, value FROM mapinfo WHERE id='".$id."' AND xy IN ({$in})";
$pullMapInfo2 = mysql_query($pullMapInfo) or die('error here');
// we create an associative array xy => value
$result = array();
while (($pullMapInfo3 = mysql_fetch_assoc($pullMapInfo2)) !== false) {
$result[ $pullMapInfo3['xy'] ] = $pullMapInfo3['value'];
}
// we make a loop to display expected output
foreach ($array as $xy)
{
if (array_key_exists($xy, $result)) {
echo '<div class="castle_array" style="background-image: url(tiles/'.$result[$xy].'.BMP)" id="'.$xy.'">'. $result[$xy] .'</div>';
} else {
echo '<div class="castle_array" id="'.$xy.'"></div>';
}
echo '<div class="clear_both"></div>';
}
?>

php javascript select

i need a help
in php i have a select box like this
<?php
$perpage = 5;
$total = 128;
$num = ceil( $total / $perpage );
$i = 1;
echo "<FORM NAME=\"form1\">";
echo "Select <SELECT NAME=\"select\" onChange=\"goto(this.form)\" SIZE=\"1\" >";
echo "<OPTION VALUE=\"\">----Select Page----";
for($i = 1; $i <= $num; $i++)
{
$sel = $i;
$goto = ($i - 1) * $perpage;
if($goto == 0) { $goto = ''; }
echo "<OPTION VALUE=\"http://localhost/CI_doctrine/blog/filmnews/" . $goto . "\">" . $i . "";
}
echo "</SELECT> Page";
echo "</FORM>";
?>
javascript code is here
function goto(form) {
var index = form.select.selectedIndex;
if (form.select.options[index].value != "0") {
window.location = form.select.options[index].value;
form.select.selected = form.select.options[index].value;
}
}
the code is working fine but i want to change the selected option to set the selected number after the page redirection but here iam getting the "select page" as the selected option
any help appreciated.
thank you from your friend.
Once you redirect, that page is unloaded and a new page loaded (even if it has the same items). When the new page loads, you want to do:
window.onload = function() {
var i, options = form.select.options, curUrl = window.location.href;
for (i = 0; i < options.length; i++) {
if (options[i].value == curUrl) {
form.select.selectedIndex= i;
break;
}
}
}
This will select the current URL. Make sure the URLs in the select options are full URLs (including http://). Also, window.onload is the DOM1 way. You should probably use a real library to deal with this.
Alternatively, you can also select the right input in PHP using the same basic approach.

Categories