I am trying to pull two results from 2 sql statements and print into HTML table.
but some how results are not coming correct.please hel me to resolve.
Below is code 2nd result is not printing correct results - duplicated results.
<?php
global $wpdb;
$result1=$wpdb->get_results("select post_name,id,cat from wp_posts where post_name like '%java%');
$result2=$wpdb->get_results("select post_name,id,cat from wp_posts where post_name like '%oracle%');
?>
<table id="table_id">
<thead>
<tr>
<th>JAVA</th>
<th>ORACLE</th>
</tr>
</thead>
<tbody>
<?php foreach($result1 as $rows1) { ?>
<?php foreach($result2 as $rows2) { ?>
<tr>
<td> <?php echo $rows1->post_name ; ?> </td>
<td> <?php echo $rows2->post_name ; ?> </td>
</tr>
<?php
}
?>
<?php
}
?>
global $wpdb;
$result1=$wpdb->get_results("SELECT post_name,id,cat FROM wp_posts WHERE post_name LIKE '%java%'");
$result2=$wpdb->get_results("SELECT post_name,id,cat FROM wp_posts WHERE post_name LIKE '%oracle%'");
After getting to objects , we put the required values in two arrays so that we can create a multi-dimensional array.
$result_one = array();
$result_two = array();
//putting values in array
foreach ($result1 as $result) {
$result_one[] = $result->post_name;
}
foreach ($result2 as $result) {
$result_two[] = $result->post_name;
}
//Creating a multi-dimensional array so that we can print two columns easily
$results = array(
$result_one,
$result_two,
);
//Initialising the variables to be used as counters and array indexes.
$i = 0; $j = 0; $k = 0; $p = 1;
//getting length of array
$array_length = count($result_one);
Now start two loops to fetch data from multi-dimensional array created.
In this case: To create the table properly we need values in this order:
$results[0][0], $results[1][0], $results[0][1], $results[1][1],
$results[0][2], $results[1][2] and so on.
So we have to create loops accordingly.
while ($i < 2) {
while ($j < $array_length) {
if (fmod($p,2)) echo '<tr>'; // So that <tr> will be printed only after two <td>
echo "<td>". $results[$i][$j]. "</td>";
if($i == 0) { $i = 1; } else { $i =0; } // Toggling values of $i from 0 to 1.
$k++;
if(fmod($k, 2) == 0){ $j ++; } // Increasing $j after 2 steps.
$p++;
if (fmod($p,2)) echo '</tr>';
}
$i ++;
}
If you need three columns:
Repeat steps one two and three so that you can create an array like this:
$results = array(
$result_one,
$result_two,
$result_three,
);
Change first while loop to
while ($i < 3)
And change the following statements as:
if (fmod($p,3)) echo '<tr>';
if($i == 0) { $i = 1; } elseif ($i == 1) {$i=2} else { $i = 0; }
if(fmod($k, 3) == 0){ $j ++; }
if (fmod($p,3)) echo '</tr>';
I hope you got better understanding now and can make the required changes yourself.
This seems complicated. But this should work. Please try.
Anyone please feel free to edit the while loop to make it simpler.
something i missed out for 3rd result array.please correct me.
$results = array(
$result_one,
$result_two,
$result_three,
);
$i = 0; $j = 0; $m = 0; $k = 0; $p = 1;
$array_length = count($result_one);
?>
<table id="table_id">
<thead>
<tr>
<th>ORACLE</th>
<th>JAVA</th>
<th>SAP</th>
</tr>
</thead>
<tbody>
<?php while ($i < 3) {
while ($j < $array_length) {
if (fmod($p,3)) echo '<tr>';
echo "<td>" . $results[$i][$j] . "</td>";
if($i == 0) { $i = 1; } elseif ($i == 1) {$i=2} else { $i = 0; }
$k++;
if(fmod($k, 3) == 0){ $j ++; }
$p++;
if (fmod($p,3)) echo '</tr>';
}
$i ++;
}
?>
Related
I've written a program which currently generates 540 different numbers.
I want to display these 540 different numbers in a columns having 50 records each(except the last column where the number of records should be less than 50) using HTML <table> tag but I'm not able to do so as there are so many loops and conditions are getting applied.
Following is my program :
<?php
function findFourElements() {
$possible_numbers = '';
$sr_no = 0;
$row_count = 0;
for ($i = 0; $i <= 9; $i++) {
for ($j = 0; $j <= 9; $j++) {
for ($k = 0; $k <= 9; $k++) {
for ($l = 0; $l <= 9; $l++) {
$possible_numbers[0] = $i;
$possible_numbers[1] = $j;
$possible_numbers[2] = $k;
$possible_numbers[3] = $l;
if((int)$possible_numbers[0] + (int)$possible_numbers[1] + (int)$possible_numbers[2] + (int)$possible_numbers[3] === 14) {
$sr_no++;
echo $sr_no. ') ' . $possible_numbers[0].$possible_numbers[1].$possible_numbers[2].$possible_numbers[3]. '<br>';
}
}
}
}
}
}
findFourElements();
?>
Please somebody help me out with my logic.
You can use this code:
<?php
function findFourElements()
{
$response = [];
$possible_numbers = '';
$sr_no = 0;
$row_count = 0;
for ($i = 0; $i <= 9; $i++) {
for ($j = 0; $j <= 9; $j++) {
for ($k = 0; $k <= 9; $k++) {
for ($l = 0; $l <= 9; $l++) {
$possible_numbers[0] = $i;
$possible_numbers[1] = $j;
$possible_numbers[2] = $k;
$possible_numbers[3] = $l;
if ((int)$possible_numbers[0] + (int)$possible_numbers[1] + (int)$possible_numbers[2] + (int)$possible_numbers[3] === 14) {
$sr_no++;
$response[] = $possible_numbers[0] . $possible_numbers[1] . $possible_numbers[2] . $possible_numbers[3];
}
}
}
}
}
return $response;
}
$response = findFourElements();
$data = array_chunk($response, 50);
$srNo = 1;
?>
<table>
<tr>
<?php foreach ($data as $key => $value) { ?>
<td>
<table>
<tr>
<th>Sr. No.</th> <th>Vehicle No.</th>
</tr>
<?php foreach ($value as $val) { ?>
<tr>
<td>
<?php echo $srNo ?>
</td>
<td>
<?php echo $val ?>
</td>
</tr>
<?php $srNo++; } ?>
</table>
</td>
<?php } ?>
</tr>
</table>
if you don't want to work with return response you can add extra code inside the function.
You should use a while statement.
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<?php
/** Calculations
(# = amount)
540/50 = 10.8 (# of rows)
50*0.8 = 40 (# of records in last row)
*/
echo "<table>";
$x = 0;
$ranNum = 0;
echo "<tr>";
while ($x < 540) {
/* a round number (1,2,3,4,5,...) %1 = 0.
So (Number/50)%1 is only 0 if it's in the multiplication table of 50*/
if ((($x/50)%1) == 0) {
echo "</tr>";
echo "<tr>";
}
$ranNum = rand(1000,9999); // random number
echo "<td id='Track".$x."'>".$ranNum."</td>";
$x++;
}
echo "</tr>";
echo "</table>";
?>
</body>
</html>
I have a table, with 12 lines (rows) and 10 columns, for columns odd lines (rows), I have one div inside of cell, with index in Id NumColum_NumLine, and for pair lines (rows), i have one select element equal for all. like this enter image description here
What i need is when i select load image on div above.
<table border="solid">
<?php
for ($i = 1; $i <= 12; $i++) {
echo "<tr>";
for ($A = 1; $A <= 10; $A++)
{
if ($i % 2 == 0)
{
echo '<td id="data">';
$songs = file('pkmlist.txt');
$options = '';
$num = 1;
foreach ($songs as $song) {
$options .= '<option value="'.$num.'" onchange="load_img('.$A.'_'.($i-1).')\">'.$song.'</option>';
$num++;
}
$select = '<select name="songs">'.$options.'</select>';
echo $select;
//echo ''.$A.'_'.($i-1).'</td>';
}
else
{
echo '<td id="Pkm"> <div id="'.$A.'_'.$i.'"> '.$A.'_'.$i.' </div> </td>';
}
}
echo "</tr>";
}
?>
</table>
OnChange don't work.
There I found some syntax error. try this ...
<table border="solid">
<?php
for ($i = 1; $i <= 12; $i++) {
echo "<tr>";
for ($A = 1; $A <= 10; $A++)
{
if ($i % 2 == 0)
{
echo '<td id="data">';
$songs = file('pkmlist.txt');
$options = '';
$num = 1;
foreach ($songs as $song) {
$options .= '<option value="'.$num.'">'.$song.'</option>';
$num++;
}
$select = '<select name="songs" onchange="load_img('.$A.'_'.($i-1).');" >'.$options.'</select>';
echo $select;
//echo ''.$A.'_'.($i-1).'</td>';
}
else
{
echo '<td id="Pkm"> <div id="'.$A.'_'.$i.'"> '.$A.'_'.$i.' </div> </td>';
}
}
echo "</tr>";
}
?>
</table>
Here's my code:
echo "<table><tr>";
$count = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
extract($row);
$data = $row['info'];
echo "<td>".$data."</td>";
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
}
echo "</tr></table>";
The above code works as:
=========
= 1 = 2 =
= 3 = 4 =
= 5 = 6 =
=========
But how do I display the info in this format?
=========
= 1 = 4 =
= 2 = 5 =
= 3 = 6 =
=========
Actually, you don't need to parse array to another form...
<?php
$a = array(1,2,3,4,5,6,7);
$c = ceil(count($a)/2);
echo "<table>";
for($i=0; $i<$c;++$i){
echo "<tr><td>{$a[$i]}</td><td>{$a[$i+$c]}</td></tr>";
}
echo "</table>";
?>
Ofcourse you need to modify this code by adding db operations (like mysql_num_rows instead of count) but it works fine: enter link description here
Thanks, guys! I appreciate all your help, but this code works perfect for me. I just want to share it with others who may find this helpful. :)
$tmp=array();
$columns=2;
$row=ceil(mysql_num_rows($result)/$columns);
for ($x =1; $x <= $columns; $x++)
for ($y = 1; $y <= $row; $y++)
$tmp[$x][$y]=mysql_fetch_array($result);
echo "<table align=center width=\"50%\">\n";
for ($y =1; $y <= $row; $y++) {
echo "<tr>";
for ($x = 1; $x <= $columns; $x++)
if (isset($tmp[$x][$y]['ID']))
echo "<td>".$tmp[$x][$y]['info']." </a></td>";
else
echo "<td></td>";
echo "</tr>\n";
}
echo "</table>\n";
A brief PSA... the mysql_ extension is depreciated and will eventually be removed. You need to start using mysqli_ now.
You need to parse your data first. Once you've done that, generating the table is easy. This should work for all data sets
echo "<table>";
$count = 1;
$col1 = $col2 = array();
$rowcount = round(mysql_num_rows($result) / 2);
while($row = mysql_fetch_assoc($result)) {
if($count > $rowcount) $col2[] = $row['info'];
else $col1[] = $row['info'];
$count++;
}
$counter = 0; // Arrays start with 0
foreach($col1 as $row) { // $col1 will always be >= $col2
$row2 = (isset($col2[$counter])) ? $col2[$counter] : "";
echo "<tr><td>" . $row . "</td><td>" . $row2 . "</td></tr>";
$counter++;
}
echo "</table>";
This is untested, but you can acheive this cleanly with 2 loops. Build up the left and right columns based on the the $count variable. Build the HTML all the way up, then echo it.
Edit: I didn't realize you were dividing the data in half and placing the first half on the left column of the table. I have changed the code below to do this.
<?php
$left = array();
$right = array();
$count = 1;
$num_rows = mysql_num_rows($result);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($count <= $num_rows/2) {
array_push($left, $row['info']);
} else {
array_push($right, $row['info']);
}
$count++;
}
$html = "<table>";
for ($i = 0; $i < $count; $i++) {
$html .= "<tr><td>"
. htmlentities($left[$i])
. "</td><td>"
. htmlentities($right[$i])
. "</td></tr>";
}
$html .= "</table>";
echo $html;
?>
Here is some php magic that could shorten that code for a bit
<?php
$array = range(1, 20);
$count = 2;
$out = array_chunk($array, ceil(count($array)/$count));
array_unshift($out, null);
$out = call_user_func_array("array_map", $out);
?>
<table>
<?php foreach ($out as $row) : ?>
<tr>
<?php foreach($row as $column) : ?>
<td><?php echo( $column ); ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
Also, i would recommend you switch your database-related code to PDO. Besides all those nice features, like auto-escaping, you can easily get an array of elements from your DB query, without all this mysql_fetch_array nonsense.
I'm making a forum in PHP. I have to display all forum categories in a table, and to do so, I have used a while loop. However, I want to have only 3 td's in every table row. To loop through the categories, I'm using a while loop with the query, so I don't think I can use modulus here.
Why can't you use modulus? Just add a counter somewhere, and if it hits % 3 == 0 reset the counter and do your stuff.
You might need to do some extra if's for first and last and stuff like that, but there is no reason not to use a modulo with a while.
$i=0;
while(guard()){
if($i % 3 == 0){
//ploing
}
$i++
}
This code will close any extra rows:
<table>
<?php
$columns = 3;
$i = 0;
while($row = mysql_fetch_array($result)){
$i++;
//if this is first value in row, create new row
if ($i % $columns == 1) {
echo "<tr>";
}
echo "<td>".$row[0]."</td>";
//if this is last value in row, end row
if ($i % $columns == 0) {
echo "</tr>";
}
}
//if the counter is not divisible by the number of columns, we have an open row
$spacercells = $columns - ($i % $columns);
if ($spacercells < $columns) {
for ($j=1; $j<=$spacercells; $j++) {
echo "<td></td>";
}
echo "</tr>";
}
?>
</table>
I haven't tested the code, but the logic should work:
<Table>
<?php
$i = 0;
while($row = mysql_fetch_array($result)){
if($i == 0){
echo"<TR>";
}
echo"<td>".$row[0]."<TD>";
$i++;
if($i == 3)
{
$i = 0;
echo"</tr>"
}
}
if($i ==1){
echo "<td></td><td></td></tr>";
}
if($i ==2)
{
echo "<td></td></tr>";
}
?>
<table>
This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 6 months ago.
I have created a for loop.
I am trying to loop through lots of members and create a row for each member in a table.
It's looping through too many times.
Is it the right type of loop to use?
<?php
for ($i = 1; $i = count($u); $i++)
{
?>
<tr>
<?php
echo "<td>$i</td>";
?>
<?php
foreach ($u as $username)
{
echo "<td>$username</td>";
}
?>
<?php
foreach ($p as $points)
{
echo "<td>$points</td>";
}
?>
</tr>
<?
}
?>
$u and $p are arrays.
Thanks
You can shorten it a bit and remove the inner loops:
<php
for ($i = 1; $i <= count($u); $i++)
{
echo '<tr>';
echo "<td>$i</td>";
echo "<td>$u[$i]</td>";
echo "<td>$p[$i]</td>";
echo '</tr>';
}
?>
$i = count($u) in the loop is what's causing the problem:
for ($i = 1; $i = count($u); $i++)
On every iteration of the loop, you're assigning count($u) to $i using a single =. Should be
for ($i = 1; $i <= count($u); $i++)
or
for ($i = 0; $i < count($u); $i++)
<?php
$count = count($u);
for ($i = 1; $i < $count; $i++) {
echo '<tr>';
echo '<td>' . $i . '</td>';
foreach ($u as $username) {
echo '<td>' . $username . '</td>';
}
foreach ($p as $points) {
echo '<td>' . $points . '</td>';
}
echo '</tr>';
}
?>
You should count $u before you loop it.