I'm probably making a very basic error but I'm quite new to this.
I have a table where I need to edit what is displayed in each box using variables but I'm having trouble with getting the outputs into the table. Experimentation helped me work out the first box but I can't get the second one working because I think the function is written incorrectly. I need a conditional loop that displays all even numbers between 10 and 20 (the code below doesn't have anything to do with even numbers at the moment I'm just trying to get it to work)
<?php
$random = rand() . srand(3034);
function loop() {
for ($i = 10; $i <= 20; $i++) {
$loop = $i;
return $loop;
}
}
echo "<table border='1'>
<tr>
<td>Box 1 - ".$random."</td>
<td>Box 2 - ".$loop."</td>
</tr>
</table> ";
?>
Any help is much appreciated.
You need to loop on the tags itself, because the return condition in the loop breaks it to only 1 iteration.
So you should do it like this:
echo "<tr>
<td>Box 1 - ".$random."</td>";
for ($i = 10; $i <= 20; $i++) {
echo "<td>Box 2 - ".$i."</td>";
}
echo"</tr>";
Additionally to my comment here is some more code:
<?php
$random = rand() . srand(3034);
function loop($randomNumber) {
for ($loop = 10; $loop <= 20; $loop++) {
echo
'<tr>' .
'<td>Box 1 - ' . $randomNumber . '</td>' .
'<td>Box 2 - ' . $loop . '</td>' .
'</tr>';
}
}
echo
'<table border="1">' .
loop($random) .
'</table>';
Try this:
<?php
function loop(){
$return = '';
for($i = 10; $i <=20; $i++){
$random = rand() . srand(3034);
if($i%2==0){
$return.='<tr>
<td>Box 1 - '.$random.'</td>
<td>Box 2 - '.$i.'</td>
</tr>';
}
}
return $return;
}
echo '<table>'.loop().'</table>';
Related
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
hello guys i need to find the differences between two different loops which is summation loop and totals loop as shown in controller code below
function summary() {
echo '<table>';
$summation=$this->select_model->sum_income("date_of_income BETWEEN '" . $start . "' AND '" . $last . "'");
foreach($summation as $sum){
echo '<tr><td>Total Income</td> <td >'.$sum['total'].'</td></tr>';
}
$totals=$this->select_model->sum_expenditure("date_of_expenditure BETWEEN '" . $start . "' AND '" . $last . "'");
foreach($totals as $total){
echo '<tr><td>Total Expenditure</td><td ><strong>'.$total['total'].'<</td></tr>';
}
//display differences here:
echo '<tr><td>'.**$sum['total']-$total['total']**.'</td></tr>'
echo '</table>';
}
You would have to create the variables outside of the loops and add (or subtract) as the loop progresses.
$sum = 0;
$total = 0;
foreach... {
$sum += $sum;
}
foreach... {
$total += $total;
}
echo $sum - $total;
If you're adding negative numbers, the math will work without having to do anything specific.
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>';
}
hi heres my attempted code, the first while statement works for the rows(works for the weights0, but i cant get it to work with the columns(heights). it works so if min_height input value is 20 and max_height is 50 the columns would look like this 20 25 30 35 40 45 50. my code currently works for the rows but not columns, can anyone help?
<?php
$rowiStep = 5;
$coliStep = 5;
// Get these
$iweightMin = $_GET["min_weight"];
$iweightMax = $_GET["max_weight"];
$iheightMin = $_GET["max_height"];
$iheightmax = $_GET["min_height"];
$iCur = $iweightMin;
$iCol = $iheightMin;
print('<table class="table">');
print('<tr ><td>Height/</br>Weight</td>');
while ($iCur <= $iweightMax) {
printf('<tr><td>%d</td></tr>', $iCur);
$iCur += $rowiStep;
}
$rowiCol = $iheightMin;
while ($iCol <= $iheightmax) {
printf('<tr><td></td>', $iCol);
$iCol += $rowiCol;
}
print ('</tr>');
print('</table>');
?>
If you're looking to print a height/weight matrix; try this:
$rowiStep = 5;
$coliStep = 5;
$output = array(
'<table>'
);
for( $row_val = $_GET['min_weight'], $row_max <= $_GET['max_weight'];
$row_val < $row_max;
$row_val += $rowistep )
{
$output[] = '<tr><td>' . $row_val . '</td>';
for( $col_val = $_GET['min_height'], $col_val <= $_GET['max_height'];
$col_val < $col_max;
$col_val += $colistep )
{
$output[] = '<td>' . $col_val . '</td>';
}
$output[] = '</tr>';
}
$output[] = '</table>';
echo implode( "\n", $output );
This will produce output like this:
|min_weight |min_height|min_height+colIStep|min_height+2colIstep|...|
|min_weight + rowIstep |min_height|min_height+colIStep|min_height+2colIstep|...|
|min_weight + 2rowIstep|min_height|min_height+colIStep|min_height+2colIstep|...|
|...|
What output are you looking for?
<?php
$values = array();
for($i=0;$i<100;$i++){
$values[$i] = "aaa" . $i;
} ?>
<table>
<?php
foreach ($values as $i => $val) {
echo "<tr><td>" . $val . "</td> </tr>";
} ?>
</table>
this generated me:
aaa1
aaa2
...
aaa50
...
aaa90
...
aaa100
how can I make two column?
aaa1 aaa50
aaa2 ....
... aaa90
aaa50 aaa100
but no:
aaa1 aaa2
aaa3 aaa4
... ....
aaa99 aaa100
a function
function sqlArr($sql){
$ret = array();
$res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
if ($res) {
while($row = mysql_fetch_array($res)){
$ret[] = $row;
}
}
return $ret;
}
a code
$temp = sqlArr("SELECT value FROM table");
$data = array();
for($i=0;$i<50;$i++){
$data[] = array($temp[$i],$temp[$i+50]);
}
unset($temp);
a template
<table border='1'>
<? foreach ($data as $i => $row): ?>
<tr>
<? foreach ($row as $cell): ?>
<td><?=$cell?></td>
<? endforeach ?>
</tr>
<? endforeach ?>
</table>
Try if this works for you. Regardless of the format of the items contained in $values, it should print the array the way you want.
<?php
$size = count($values);
$halfSize = intval(ceil($size / 2));
for ($i = 0; $i < $halfSize; $i++)
{
$j = $i + $halfSize;
echo "<tr><td>" . $values[$i] . "</td>";
// If $size is odd, $halfSize is rounded up. This means that if we have
// 3 elements, it will try to access to $values[3], since $halfSize would
// be 2. The last second column element should be blank in this case.
// So, if $halfSize is odd, and $i is the last element, don't print an
// additional table cell.
if (!($i == $halfSize - 1 && $size % 2 == 1))
echo "<td>" . $values[$j] . "</td>";
echo "</tr>";
}
?>
The way that I would do this is to create two separate tables (each one column wide) and then include both of them in a single, two-columned table:
<?php
$list=array('a','b','c','d','e','f');
$midpoint=floor(count($list)/2);
$tableHeader='<table width="100%">';
$tableFooter='</table>';
$leftTable=$tableHeader;
$rightTable=$tableHeader;
for ($c=0; $c<$midpoint; $c++)
{
$leftTable.='<tr><td width="100%">'.$list[$c].'</td></tr>';
}
$leftTable.=$tableFooter;
for ($c=$midpoint; $c<count($list); $c++)
{
$rightTable.='<tr><td width="100%">'.$list[$c].'</td></tr>';
}
$rightTable.=$tableFooter;
$mainTable='<table><tr><td width="50%">'.$leftTable.'</td><td width="50%">'.$rightTable.'</td></tr></table>';
echo $mainTable;
?>
Add some CSS to remove padding around the sub-tables and borders etc and you should be good to go.
Since you already numbered it, just abstract it down:
a1 a51
a2 a52
...
a49 a99
a50 a100
is exactly the same as
a1 a1+50
a2 a2+50
...
a50 a50+50
This makes you having the following code:
echo "<table>";
for($i=1; $i<=50; $i++) {
echo "<tr><td>" . $i . "</td><td>" . ($i+50) . "</td></tr>";
}
echo "</table>";
Note: This requires you to let the table be generated this way. If it's actually not numbered like that, you would have to figure out another way to generate the abstract table in my listing 2 (just think about the index the entries have in relation to their left neighbor).