how to store data to mysql in array multiple - php

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);

Related

How to echo a table in two piece separated code in PHP?

Now I wish to construct a list of tables under nested while loop and if-else condition with PHP, the format of the code is as below:
while(){
if (){
..... // extract the data
while(){
construct a table using the data extracted above
}
}
elseif (){
..... // extract the data
while(){
construct a table using the data extracted above
}
}
}
Specifically, in the inner while loop the code is:
while ( $chat = mysqli_fetch_assoc($chatQ))
{
echo
"<table class='table table-hover' >"
."<td>"
.$conver['sender_name']."\t".$chat['sender']."\t".$chat['send_time']."\t".$chat['content']
."</td>"
."<td>"
."<form id='join' action='group_chat.php' method ='POST' accept-charset='UTF-8'>"
// post the group id
."<input type='hidden' name='group_id' id='group_id' value=".$conver['sender_id']."/>"
."<button class='btn btn-default' type='submit'>Enter</button>"
."</form>"
."</td>"
."</table>";
}
But the result is very ugly:
The problem is that the Enter button is associated with each message. But what I want is that after displaying all the messages, there is a Enter button which can direct to the specific group. But I don't know how to separate the code. Could you please do me a favor? Thanks in advance!
You can do something like this:
<?php
$counter = 0;
echo '
<form>
<table>';
while ( $chat = mysqli_fetch_assoc($chatQ)){
$counter++;
echo '
<tr>
<td>
group_id_'.$counter.'
</td>
<td>
<input name="group_id_'.$counter.'" value="'.$conver['sender_id'].'">
</td>
</tr>';
}
echo '
<tr>
<td colspan="2">
<button type="submit">Enter</button>
</td>
</tr>
</table>
</form>';
?>
Also you dont have to limit yourself to echo everything in order by setting variables. See example below:
<?php
$counter = 0;
$data = NULL;
while ( $chat = mysqli_fetch_assoc($chatQ)){
$counter++;
$data .='
<tr>
<td>
group_id_'.$counter.'
</td>
<td>
<input name="group_id_'.$counter.'" value="'.$conver['sender_id'].'">
</td>
</tr>';
}
echo '
<form>
<table>'.$data.'
<tr>
<td colspan="2">
<button type="submit">Enter</button>
</td>
</tr>
</table>
</form>';
?>

How Can I nest two foreach to get right values output?

I have two foreach blocks to get some values from a web page (I'm scraping values from an HTML table).
<?php
include('../simple_html_dom.php');
$html = file_get_html('http://www.betexplorer.com/soccer/belgium/jupiler-league/results/');
foreach($html->find('td') as $e) {
echo $e->innertext . '<br>';
}
foreach( $html->find('td[data-odd]') as $td ) {
echo $td->attr['data-odd'].PHP_EOL;
}
?>
and this is my HTML code:
<tr class="strong">
<td class="first-cell tl">
Waasland-Beveren - Anderlecht
</td>
<td class="result">
1:0
</td>
<td class="odds best-betrate" data-odd="5.97"></td>
<td class="odds" data-odd="4.21"></td>
<td class="odds" data-odd="1.51"></td>
<td class="last-cell nobr date">21.02.2016</td>
</tr>
<tr class="">
<td class="first-cell tl">
Waregem - KV Mechelen
</td>
<td class="result">
2:3
</td>
<td class="odds" data-odd="1.83"></td><td class="odds" data-odd="3.71"></td>
<td class="odds best-betrate" data-odd="3.99"></td>
<td class="last-cell nobr date">21.02.2016</td>
</tr>
In this way, in my output, I get before values from the first foreach and, after, values from the second. I'd like to get values together in the right order. For example:
21.02.2016 Waasland-Beveren - Anderlecht 1:0 5.96 4.20 1.51
21.02.2016 Waregem - KV Mechelen 2:3 1.83 3.71 3.98
If they have the exact (count) or (length). Use the normal for after you assign them to two variables.
$td = $html->find('td');
$attr = $html->find('td[data-odd]');
for($i=0; $i < count($td); $i++)
echo $td[$i]->innertext."<br/>".$attr[$i]->attr['data-odd'].PHP_EOL;
Update:
You want to reorder the tds you received from the HTML file, that means you have to think of another logic in how to retrieve them. This updated code is very specific to your case:
$match_dates = $html->find("td[class=last-cell nobr date]"); // we have 1 per match
$titles = $html->find("td[class=first-cell tl]"); // 1 per match
$results = $html->find("td[class=result]"); // 1
$best_bets = $html->find("td[class=odds best-betrate]"); // 1
$odds = $html->find("td[class=odds]"); // 2
// Now to output everything in whatever order you want:
$c=0; $b=0; // two counters
foreach($titles as $match)
echo $match_dates[$c]->innertext." - ".$match->innertext." [".$results[$c]->innertext."] - Best bet rate: ".$best_bets[$c++]->attr['data-odd']." - odds: ".$odds[$b++]->attr['data-odd'].", ".$odds[$b++]->attr['data-odd']."<br/>";

Assign variable to array values and populate <td>

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>

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.

While loop and mysql_fetch_assoc on one query?

I have a webpage that does a MySQL query on a table. I have it to echo it out in a table and that works as it should, example:
$query = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_array($query){
// my table and db stuff echos out here
}
Now using the same mysql query $query I am trying to echo it out again on the same page using the same query underneath but my problem is it does not seem to work.
Now you maybe thinking what I'm doing is odd, but the reason why is the first code above echoes data out in a table, but also checkboxes next to the stuff echoed out as it is for a form. All that works fine but it seems I cannot do another while loop above on the same query. The second one is exactly the same as above; only difference is it's not a form.
Can I only do a while() and mysql_fetch_assoc once on a single query?
UPDATE:
I'm sorry I still don't understand properly.
Here's my code; could anyone edit it for me?
(I could not put php tags in the code to split up the HTML from the PHP code. Sorry for any inconvenience).
$q = mysql_query("SELECT * FROM table");
<h1> Vote for your favourite extension </h1>
<form method="post" action="<?php echo basename(__file__); ?>">
<table>
<tbody>
<tr class="odd">
<td colspan="3" class="cellfeat" style="text-align: center;">Vote for your favourite extension</td>
</tr>
<?php
if(!$q){
// query failed etc
} else { // query ok so display form
while($row = mysql_fetch_array($q)){
echo '<tr class="odd">';
echo '<td class="cellfeat"><img src="images/statimages/extensions.gif" alt="Extension Vote Image" /></td>';
echo '<td class="cellfeat">'.$row['checkbox'].'</td>';
echo '<td class="cellfeat"><input type="checkbox" name="'.$row['id'].'" value="'.$row['id'].'" /></td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
<input type="submit" class="submitcontact" value="Vote" />
</form>
<h1>Extension Statistics</h1>
<table>
<tbody>
<tr class="odd">
<td colspan="3" class="cellfeat" style="text-align: center;">Voting Statistics</td>
</tr>
while($row = mysql_fetch_array($q)){
echo '<tr class="odd">';
echo '<td class="cellfeat"><img src="images/statimages/extensions.gif" alt="Extension Vote Image" /></td>';
echo '<td class="cellfeat">'.$row['checkbox'].'</td>';
echo '<td class="cellfeat">'.$row['count'].'</td>';
echo '</tr>';
}
?>
</tbody>
</table>
The only proper way as follows:
first, collect your data into array
$data = array();
while($row = mysql_fetch_array($query){
$data[] = $row;
}
then, call a template
and use your data as many times as you wish:
<table>
<? foreach ($data as $row): ?>
<tr>
<td><?=$row['id']?></td>
<td><?=$row['name']?></td>
<? endforeach ?>
</table?>
If I understand you need to use this function
http://php.net/manual/en/function.mysql-data-seek.php
to move your pointer

Categories