PHP Dynamic <FORM> name = col'.$i.' - php

hope someone can help me with my problem.
Im generating a Form with a dynamic amount of Dropdown/
while($length<count($spalten)) {
echo '<TD>
<SELECT ID="col['.$length.']" name="col['.$length.']">';
$selected = 0;
echo $length;
foreach( $spalten AS $value){
if($selected != $length){
echo '<OPTION value='.$value.'>'.$value.'</OPTION>';
}
else{
echo '<OPTION SELECTED value='.$value.'>'.$value.'</OPTION>';
}
$selected++;
}
echo ' </SELECT></TD>';
$length++;
} ...
And than i try to get the values for them:
for( $i = 0; $i < $cols; $i++){
$insert = $insert.', $col'.$i.'='.$r[$i].' ' ;
}
but $col'.$$i.' does not work of course, but i dont know how to handle that? how can i get the value of "$col0" and get it by $col$i???
hope someone knows
Thx so far
// EDIT
yeah sorry
$insert = is sopposed to be a MySQL Insert Query
$q_fillreport = new DB_QS;
$q_fillreport->query("INSERT INTO $typname
SET reportid = $Id
$insert;");
}
and $col1 -> $colX are $_POST values from the
<SELECT ID="col['.$length.']" name="col['.$length.']">';

Related

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 retain selected item of ddl in php?

I have three drop down lists and I need to retain selected item in ddl after form is submitted
e.g. When I click 09 in ddl and submit form then it should display 09 in ddl. Pasting my code for your reference.
echo"<option> Select Day</option>";
for ($i = 1; $i <= 31; $i++)
{
if($i<10){
echo "<option>".str_pad($i,2,"0",STR_PAD_LEFT)."</option>";
}
else{
echo "<option>".$i."</option>";
}
}
?>
I don't know what your DDL name attribute is, so I'll pretend
<select name="ddl"­>
Here's what you code should look like:
echo"<option> Select Day</option>";
for ($i = 1; $i <= 31; $i++)
{
if(isset($_POST['ddl']) && $_POST['ddl'] == $i)
$select = " checked='checked'";
else
$select = "";
if($i<10){
echo "<option " . $select . ">".str_pad($i,2,"0",STR_PAD_LEFT)."</option>";
}
else{
echo "<option " . $select . ">".$i."</option>";
}
}
?>
This is a base example, you should take measure to protect your code against injection.

variable increment doesn't work

When I launch my web page, increment doesn't work correctly!
It should go like this: $i = from 1 to x (0,1,2,3,4,5,6 etc..).
But instead it jumps over every step giving result of (1,3,5,7 etc..).
Why is this code doing this?
<ul class="about">
<?php
$result = mysql_query("SELECT * FROM info WHERE id = 1");
while ($row = mysql_fetch_assoc($result))
{
$bioText = $row['bio'];
}
$endBioTxt = explode("\n", $bioText);
for ($i=0; $i < count($endBioTxt);)
{
if (checkNum($i) == true)
{
echo "<li class='left'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
else
{
echo "<li class='right'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
$i++;
}
// Function to check if number is prime
function checkNum($num){
return ($num % 2) ? TRUE : FALSE;
}
?>
</ul>
Output:
Sometext!(right side)
0
1
Sometext2!(right side)
2
3
...
Please DONT do this as other suggested:
for ($i=0; $i < count($endBioTxt); $i++)
do this:
$count = count($endBioTxt);
for ($i=0; $i < $count; $i++) {
}
No need to calculate the count every iteration.
Nacereddine was correct though about the fact that you don't need to do:
$i++;
inside your loop since the preferred (correct?) syntax is doing it in your loop call.
EDIT
You code just looks 'strange' to me.
Why are you doing:
while ($row = mysql_fetch_assoc($result))
{
$bioText = $row['bio'];
}
???
That would just set $bioText with the last record (bio value) in the recordset.
EDIT 2
Also I don't think you really need a function to calculate the modulo of a number.
EDIT 3
If I understand your answer correctly you want 0 to be in the left li and 1 in the right li 2 in the left again and so on.
This should do it:
$endBioTxt = explode("\n", $bioText);
$i = 0;
foreach ($endBioTxt as $txt)
{
$class = 'left';
if ($i%2 == 1) {
$class = 'right';
}
echo '<li class="'.$class.'"><div>'.$txt.'</div></li>';
echo $i; // no idea why you want to do this since it would be invalid html
$i++;
}
Your for statement should be:
for ($i=0; $i < count($endBioTxt); $i++)
see http://us.php.net/manual/en/control-structures.for.php
$i++; You don't need this line inside a for loop, it's withing the for loop declaration that you should put it.
for ($i=0; $i < count($endBioTxt);$i++)
{
if (checkNum($i) == true)
{
echo "<li class='left'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
else
{
echo "<li class='right'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
//$i++; You don't need this line inside a for loop otherwise $i will be incremented twice
}
Edit: Unrelated but this isn't how you check whether a number is prime or not
// Function to check if number is prime
function checkNum($num){
return ($num % 2) ? TRUE : FALSE;
}
This code works, please test it in your environment and then uncomment/comment what you need.
<?php
// This is how query should look like, not big fan of PHP but as far as I remember...
/*
$result = mysql_query("SELECT * FROM info WHERE id = 1");
$row = mysql_fetch_assoc($result);
$bioText = $row['bio'];
$endBioTxt = explode("\n", $bioText);
*/
$endBioTxt[0] = "one";
$endBioTxt[1] = "two";
$endBioTxt[2] = "three";
$endBioTxt[3] = "four";
$endBioTxt[4] = "five";
$totalElements = count($endBioTxt);
for ($i = 0; $i < $totalElements; $i++)
{
if ($i % 2)
{
echo "<li class='left'><div>".$endBioTxt[$i]."</div></li>";
}
else
{
echo "<li class='right'><div>".$endBioTxt[$i]."</div></li>";
}
/*
// This is how you should test if all your array elements are set
if (isset($endBioTxt[$i]) == false)
{
echo "Array has some values that are not set...";
}
*/
}
Edit 1
Try using $endBioTxt = preg_split('/$\R?^/m', $bioTxt); instead of explode.

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