Assign variable to array values and populate <td> - php

I have the following code inside a WHILE loop following a MySQL query:
$values=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30);
echo "<tr>
<td class='results'>$values</td>
<td class='results'>working query</td>
</tr>"
I need the $values variable to populate and increase by 1 for each row obtained from the query.
Desired result:
1 | data
2 | data
3 | data

You didn't post the while loop, so I just assume how it looks like. You can just add a simple counter variable and increase for every row returned from DB:
$i = 1;
while ($row = mysqli_fetch_assoc($res)) {
echo "<tr>
<td class='results'>$i</td>
<td class='results'>working query</td>
</tr>";
$i++;
}

use that code please
<table>
<?php
$values=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30);
foreach($values as $v){
echo "<tr>
<td class='results'>$v</td>
<td>|</td>
<td class='results'>working query</td>
</tr>";
}
?>
</table>

Related

i want to calculate subtraction of row2 & row1

I display mysql table data using php.
I search but find solution for column but not for row.
Below I try to show what I want...
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<table>
<tr>
<td id="<?php echo $row["id"]; ?>"><?php echo $row["total"]; ?></td>
<td id="difference"> Difference from previous row. </td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
$i=0;
$oldval = 0;
while($row = mysqli_fetch_array($result)) {
?>
<table>
<tr>
<td id="<?php echo $row["id"]; ?>"><?php echo $row["total"]; ?></td>
<td id="difference"> <?php echo ($i==0) ? $oldval : $row['total']-$oldval; ?> </td>
</tr>
<?php
$oldval = $row["total"];
$i++;
}
?>
</table>
$oldval variable is used to store current row's total field data so when you go to the next row, you can get the difference for current row because you have previous row's data stored in the $oldval
And the ternary condition I put there is because if $i==0 means the first row so you don't have any data of the previous row so by default difference is 0 and you can notice that I stored the current record's total field data after printing the difference

how to store data to mysql in array multiple

this is my form
how to create an array for every row like('algo'=>'input1','algo'=>'input2'
then ('algo2'=>'input1','algo2'=>'input2')
i want to do this for dynamic number of topics and also want to insert in mysql
through php. this is how i want to store the array values
$sel=mysqli_query($db,"SELECT * from srs");
$count=mysqli_num_rows($sel);
while ($row=mysqli_fetch_array($sel))
{
echo '<tr>
<td class="col-sm-2">'.$row['name'].'</td>
<td class="col-sm-2"> / </td>
</tr>';
$i++;
}
Try like this,
$new_array =array(); //array initialzise
$sel=mysqli_query($db,"SELECT * from srs");
$count=mysqli_num_rows($sel);
$i=0;
while ($row=mysqli_fetch_array($sel))
{
$new_array[]=$row["topic_name"];
echo '<tr>
<td class="col-sm-2">'.$row["name"].'</td>
<td class="col-sm-2"> / </td>
</tr>';
$i++;
}
print_r($new_array);

Why oci_fetch_array($stid) don't return the first row of oracle database table

I'm trying to make a search form that return some data from an oracle database,it works fine except that it skip the first row of table .
I used the following code :
enter code here
if(isset($_POST['search']) && !empty($_POST["search_box"])){
$name=$_POST['search_box'];
$sql='SELECT deejays.name,available_dates.data, available_dates.venue,available_dates.location FROM deejays,available_dates';
$sql .=" WHERE deejays.name ='{$name}'";
$sql .=' AND pk_id=fk_id';
$verify="SELECT deejays.name FROM deejays WHERE deejays.name ='{$name}'";
$stid = oci_parse($conn,$sql );
oci_execute($stid);
if (oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)){
echo "<table width=\"100%\" align=\"center\" cellpadding=\"5\" cellspacing=\"5\">
<tr class=\"source\">
<td colspan=3 align=\"center\"><h1>The facebook tour dates for ".$name." are:</h1></td>
</tr>
<tr align=\"center\" class=\"source1\">
<td><strong>DATA</strong></td>
<td><strong>VENUE</strong></td>
<td><strong>LOCATION</strong></td>
</tr>";?>
<?php while($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)){ ?>
<tr align="center" class="rows">
<td ><?php echo $row['DATA'];?></td>
<td ><?php echo $row['VENUE'];?></td>
<td ><?php echo $row['LOCATION'];?></td>
</tr>
<?php } ?>
</table>
<?php } else {
$stid = oci_parse($conn,$verify );
oci_execute($stid);
if (oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
{ echo "This artist hasn't records on facebook/bandsintown<br>
enter code here
the expected result is:
and the real result is:
The second image shows that the first row is skipped,how could I solve this?Thanks in advance
As I commented, oci_fetch_array() will return an array containing the next result-set row of a query. Which means everytime you call it, the cursor will move to the next row. I have two suggestions.
First
Load all the result into an array after oci_execute(). And count the array if it has any row.
oci_execute($stid);
$result = array();
while($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
{
$array[] = $row;
}
//check if result is not empty
if (count($result)) {
//the table part here.
}
And when you want to display it in html table, use foreach loop to iterate the array and build the <tr>.
<?php foreach($result as $row) { ?>
<tr align="center" class="rows">
<t ><?php echo $row['DATA'];?></td>
<td><?php echo $row['VENUE'];?></td>
<td><?php echo $row['LOCATION'];?></td>
</tr>
<?php } ?>
SECOND
You can use oci_fetch_all() to load all the rows from the query into an array.
oci_execute($stid);
//$result will have all the result of the current query
$count = oci_fetch_all($stid, $result);
if ($count) {
//table part here
}
And building the <tr> will be the same as the first.
Another way is to use buffered query. But this will depend on many factors.

changing td's color with if statement

I'm trying to use if-statement to change <td>-s color.
Basically, I have a simple query to retrieve information from the database.
Additionaly, I have a column there which keeps the information like if the task is accomplished or not. When I retrieve the information I get all of them, but I need the accomplished tasks to be green, and others without any color.
I've searhed for the answer, but I couldn't find anything that satisfies me.
For example:
$qry = mysql_query("select * from table");
$recs = array();
while($row = mysql_fetch_array($qry))
$recs[]=$row;
mysql_free_result($qry);
I've tried to add while statement to the code above, but I was confused and it didnt work :(
I'm printing the results using heredoc:
How to give them color here?
<?php
$last_id=0;
foreach($recs as $rec)
{
$html=<<<HTML
<tr>
<td><b>Номер</b></td>
<td>$rec[0]</td>
</tr>
<tr>
<td><b>Номер документа</b></td>
<td>$rec[1]</td>
</tr>
<tr>
<td><b>Дата регистрации</b></td>
<td>$rec[8]</td>
</tr>
<tr>
<td><b>От кого</b></td>
<td>$rec[2]</td>
</tr>
<tr>
<td><b>По</b></td>
<td>$rec[4]</td>
</tr>
<tr>
<td><b>Краткое содержание</b></td>
<td>$rec[3]</td>
</tr>
<tr>
<td><b>Исполнитель</b></td>
<td>$rec[5]</td>
</tr>
<tr>
<td><b>Срок исполнения</b></td>
<td>$rec[6]</td>
</tr>
<tr>
<td><b>Срок исполнения продлен до</b></td>
<td><b>$rec[10]</b></td>
</tr>
<tr>
<td><b>Прислан</b></td>
<td>$rec[9]</td>
</tr>
<tr>
<td><b>Примечание</b></td>
<td>$rec[7]</td>
</tr>
<tr>
<td bgcolor="#838B83"> </td>
<td bgcolor="#838B83"> </td>
</tr>
HTML;
print $html;
if($rec[0]>$last_id)
$last_id=$rec[0];
};
$new_id=$last_id+1;
?>
rather than colour use a class, so you can change it in CSS
<td<?php if($row['complete']) echo ' class="complete"'; ?>>data</td>
<table>
<tr>
<td>column heading</td>
</tr>
<?php
$qry=mysql_query("select * from table");
while($row=mysql_fetch_array($qry)) {
if($row['urcolumnn']==1)
{
echo "<tr bgcolor=green>";
}
else
{
echo "<tr>";
}
?>
<td>
<?php echo $row['urcolumn']; ?>
</td>
</tr>
<?php } ?>
</table>
This is an example code. think this will help you. here i give the background color to <tr> like this if u want to give color to <td> use this <td style="background-color:green;">
I would suggest you to use ternary operator, rather than using IF statement, or your could use another workaround to use arrays to define colors, this would help you to define various colors for each status value globally, please find an example below:
$aryColor = array(
'complete' => 'green',
'incomplete' => "red",
'pending' => 'orange'
.....
);
//you can specify values for all of your status, or leave them blank for no color, the keys in the array are the possible values from your status field
foreach($recs as $rec) {
echo '<tr bgcolor="'.$aryColor[$rec['status_field']].'">
<td>'.$rec['title_field'].'</td>
</tr>';
}
I hope this helps you out, and you can easily edit this for HEREDOC.
first you should change your column values change its structure to int and set default value as "0".so when your task is complete the field value should be change to "1".
now come to your code:
$qry = mysql_query("select * from table");
while($row = mysql_fetch_assoc($qry)){
if($row['status']==1){
echo "<td color="green">Data</td>"
}
else{
echo "<td color="white">Data</td>";
}
}
Hence you can get the rows in green which status is==1 means complete and white for incomplete.

Put records next to eachother in columns using mysql_fetch_array?

I use mysql_fetch_array to fetch data from a mysql results set:
while($row = mysql_fetch_array($qry_result)){
Next I would like to place the data into columns in a table, but maximum of two columns at each row.
So using a table:
<table>
<tr>
<td>Record Here</td>
<td>Record Here</td>
</tr>
<tr>
<td>Record Here</td>
<td>Record Here</td>
</tr>
<tr>
<td colspan="2">Record Here</td>
</tr>
As you see above, I want to loop the results and create table columns in the loop.
This so that the records line up two and two on a results page.
Remember, if there is an odd number of records, then the last table column would need a colspan of 2, or perhaps just use an empty column?
Anybody know how to do this?
If I use a for loop inside the while loop, I would just be lining up the same records x times for each while loop. Confusing...
Any ideas?
Thanks
Implement a (1-indexed) counter within the while loop. Then add (after the loop):
if ($counter%2)
echo '<td></td>';
This will leave an additional blank cell in your table if the last column contains only one row.
Something like this should work...
<table>
<?php
while(true) {
$row1 = mysql_fetch_array($qry_result);
if($row1 === false) break;
$row2 = mysql_fetch_array($qry_result);
if($row2 !== false) {
echo "<tr><td>$row1</td><td>$row2</td></tr>";
} else {
echo "<tr><td coslspan=\"2\">$row1</td></tr>";
break; // output the final row and then stop looping
}
}
?>
</table>
<table>
<tr>
<?
$i=1;
$num = mysql_num_rows($qry_result);
while($row = mysql_fetch_array($qry_result)){
if($i%2==1)
?>
<tr>
<?
?>
<td <? if($i==$num && $i%2==1)echo 'colspan="2"';?>><?=$row['keyName']?></td>
<?
if($i%2==0)
{
<?
</tr>
?>
}
$i++;
}
?>

Categories