Yesterday I asked a question about array, with commas, output as column.
Check it here
Today I have come this far:
<?php require_once("dbconnection.php"); ?>
<?php
$sql = "SELECT amount, ingredients FROM opskriftreg LIMIT 1;";
$ingredients = explode(',', $row['ingredients']);
$amount = explode(',', $row['amount']);
echo '<table>';
for ($i = 0; $i < count($ingredients); $i++) {
echo '<tr>';
echo '<td>', $ingredients[i], '</td>';
echo '<td>', $amount[i], '</td>';
echo '</tr>';
}
echo '</table>';
?>
The site shows an empty table now, but no errors, no nothing.
Anybody got any ideas??
You didn't executed the query..
$sql = "SELECT amount, ingredients FROM opskriftreg LIMIT 1;";
// $result = mysqli_query($mysqli_link, $sql);
// $row = mysqli_fetch_array($result);
$ingredients = explode(',', $row['ingredients']);
$amount = explode(',', $row['amount']);
Go to mysqli_query for more help.
Related
I am trying to display SQL table data with PHP, right now I have made an HTML table that infinitely loops up until the end of the page. Now I want to try and display 10~ names vertically before starting a new HTML table next to it that continues looping until the entire SQL table is displayed. The script I have right now is as follows.
$result = mysqli_query($conn, "SELECT name, id FROM table ORDER BY name");
while($row = mysqli_fetch_array($result)){
echo "<table>
<tr><td><input type=\"submit\" name=\"select\" value=\"" . $row['name'] . "\"></input></td></tr>
</table>" ;
}
if I understood correctly you want to have a table for each 10 rows , if thats the case you can do it as follows :
$result = mysqli_query($conn, "SELECT name, id FROM table ORDER BY name");
$i = 1;
while ($row = mysqli_fetch_array($result)) {
if ($i == 1) {
print "<table>";
} elseif ($i == 10) {
print " </table><table>";
$i = 1;
}
echo "
<tr><td><input type=\"submit\" name=\"select\" value=\"" . $row['name'] . "\"></input></td></tr>
";
$i++;
}
You can simply fetch all the rows into a table and then split it into chunks of 10.
$result = mysqli_query($conn, "SELECT name, id FROM table ORDER BY name");
$data = $result->fetch_all(MYSQLI_ASSOC);
foreach (array_chunk($data, 10) as $table) {
echo "<table>";
foreach ($table as $row) {
echo "<tr><td><input type=\"submit\" name=\"select\" value=\"" . $row['name'] . "\"></input></td></tr>";
}
echo "</table>";
}
I'm trying to display information from two queries in one single table, but can't figure out how to make it work.
This is what I got working with one query:
SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC;
Company Employees
ABC 45
DEF 15
GHI 5
Now beneath that I'd like to have another query that simply counts all rows, giving me the total amount of Employees.
SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC;
SELECT COUNT(*) AS Total FROM Employee_Table;
Company Employees
ABC 40
DEF 15
GHI 5
Total 60
This is what my code looks like right now. I defined two extra variables for my extra query that I want to echo out, but believe this is not the proper way to do it as I get an sqlsrv_fetch_array error.
$query1 = "SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC;";
$query2 = "SELECT COUNT(*) AS Totaal FROM Employee_Table;";
$result1 = sqlsrv_query($conn, $query1);
$result2 = sqlsrv_query($conn, $query2);
echo "<table id='total'>";
echo "<tr><th>Company</th><th>Amount of employees</th></tr>";
while ($row=sqlsrv_fetch_array($result1, $result2)) {
echo "<tr><td>";
echo $row["Company"];
echo "</td><td>";
echo $row["count"];
echo "</td><td>";
echo $row["Total"];
echo "</td></tr>";
}
echo "</table>";
How can this be achieved? I'd appreciate any help
First correct
sqlsrv_fetch_array()
see here http://php.net/manual/de/function.sqlsrv-fetch-array.php
Use just a single query and use mysqli_num_rows() function to count
$count = mysqli_num_rows($result);
I think you can achieve this by using only first query
$query1 = "SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC;";
$result1 = mysql_query($conn, $query1);
$total_employee = 0;
echo "<table id='total'>";
echo "<tr><th>Company</th><th>Amount of employees</th></tr>";
while ($row=mysql_fetch_array($result1)) {
echo "<tr><td>";
echo $row["Company"];
echo "</td><td>";
echo $row["count"];
$total_employee += $row["count"];
echo "</td><td>";
echo "</td></tr>";
}
echo "<tr><td>Total</td><td>$total_employee</td></tr>";
echo "</table>";
You can use only one query:
$result = sqlsrv_query($conn, "SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC");
?>
<table id='total'>
<tr><th>Company</th><th>Amount of employees</th></tr>
<?php
$total = 0;
while ($row=sqlsrv_fetch_array($result)) {
$total += $row["count"];
?>
<tr><td><?= $row["Company"] ?></td><td><?= $row["count"] ?></td></tr>
<?php
}
?>
<tr><td></td><td><?= $total ?></td></tr>
</table>
<?php
Try the following code
$query1 = "SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC;";
$query2 = "SELECT COUNT(*) AS Totaal FROM Employee_Table;";
$result1 = sqlsrv_query($conn, $query1);
$result2 = sqlsrv_query($conn, $query2);
echo "<table id='total'>";
echo "<tr><th>Company</th><th>Amount of employees</th></tr>";
while ($row=sqlsrv_fetch_array($result1)) {
echo "<tr><td>";
echo $row["Company"];
echo "</td><td>";
echo $row["count"];
echo "</td><td>";
echo "</td></tr>";
}
$rows = sqlsrv_fetch_array($result1)
echo "<tr><td>";
echo $rows["Total"];
echo "</td></tr>";
echo "</table>";
Hello i'm a (beginning) php backend dev and i'm working on a dj panel but it doesnt work the right way i tried as many things as i could but i cant get it to work..
$active_ids = '1, 3, 4';
$query = "SELECT * FROM users WHERE id IN ({$active_ids})";
$result = $mysqli->query($query);
$query2 = "SELECT dj, count(*) AS n FROM timetable WHERE dj IN ({$active_ids}) GROUP BY dj";
$result2 = $mysqli->query($query2);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td>", $row['username'] ,"</td>";
}
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()){
echo "<td>", $row2['n'] ,"</td>";
echo "</tr>";
}
}
}
this is what it shows
ZOMBOY
Hater
ZOMBOY2 3
1
1
and this is how it needs to become but i cant find a way to do it
ZOMBOY 3
Hater 1
ZOMBOY2 1
You can use join instead to querying two table
$active_ids = '1, 3, 4';
$query = "SELECT u.username, count(*) AS n FROM users u, timetable tt WHERE u.id=tt.dj and u.id IN ({$active_ids}) GROUP BY tt.dj";
$result = $mysqli->query($query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td>", $row['username'] ,"</td>";
echo "<td>", $row['n'] ,"</td>";
echo "</tr>";
}
}
You can do it like this, but must be look Joins
$active_ids = '1, 3, 4';
$query = "SELECT * FROM users WHERE id IN ({$active_ids})";
$result = $mysqli->query($query);
$query2 = "SELECT dj, count(*) AS n FROM timetable WHERE dj IN ({$active_ids}) GROUP BY dj";
$result2 = $mysqli->query($query2);
$columnOne = Array();
$columnTwo = Array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
$columnOne[]= $row['username'];
}
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()){
$columnTwo[] = row2['n'];
}
}
}
echo '<table>';
for($i=0;$i<count($columnOne);$i++){
echo '<tr><td>' . $columnOne[$i] . '</td><td>' . $columnTwo[$i] . '</td></tr>';
}
echo '</table>';
You could try something such as this (Not quite as elegant as others):
# Escape your characters
$active_ids = "'1', '3', '4'";
# Tidy up the querys to reduce the change of reserved words being used
$query = "SELECT * FROM `users` WHERE `id` IN ({$active_ids});";
$result = $mysqli->query($query);
$query2 = "SELECT `dj`, COUNT(*) AS n FROM `timetable` WHERE `dj` IN ({$active_ids}) GROUP BY `dj`";
$result2 = $mysqli->query($query2);
# Count your results
$c1 = count($result);
$c2 = count($result2);
#Set the counter to be the larger of the 2
$counter = (($c1 > $c2) ? $c1 : $c2);
if ($result->num_rows > 0 && $result2->num_rows > 0)
{
# Print the table opener
print '<table class="your_class">';
# Loop through your results
for ($i = 0; $i < $counter; $i++)
{
# Print the data needed
print '<tr><td>' . $result[$i]['username'] . '</td><td>' . $result2[$i]['n'] . '</td></tr>';
}
# End the table
print '</table>';
}
So What I want to do is to echo 1 for the first row that is returned and two for the 2nd one and so one, Please I just don't know how to phrase this, I don't want to just count rows from mysql table, I want to give each row a number automatically and echo that number.
Please forgive my deprecated code.
$log = mysql_query("SELECT * FROM $table WHERE $columnid = '$id'") or die (mysql_error());
while($row = mysql_fetch_array($log)){
echo row['name'];
}
Lets assume that the above returns some names. so I want this.
1 : John
2 : Nancy
3 : Dave
I don't want to write those number.
Please take into account ORDER by rate DESC since I'm using that to descent the number by the biggest number of rate.
You could do:
$count = 1;
while($row = mysql_fetch_array($log)){
echo $count . ' ' . row['name'];
$count++;
}
Or:
echo '<ol>';
while($row = mysql_fetch_array($log)){
echo '<li>' . row['name'] . '</li>';
}
echo '</ol>';
$log = mysql_query("SELECT * FROM $table WHERE $columnid = '$id'") or die (mysql_error());
$c = 0
while($row = mysql_fetch_array($log)){
echo row['name'];
$c = $c + 1;
}
echo "Raw Count = ".$c
I have 2 separate arrays being pulled from MYSQL database, and want to combine both arrays on the key, so I can use a php foreach statement.
*I tried doing 1 query with both, but could not figure out how to get my foreach $video[0] to work.*
match_descr = video code
m_date = date
SQL
$query = "SELECT match_descr FROM #__bl_match WHERE match_descr != '' ORDER BY m_date DESC";
$db->setQuery($query);
$videomatch = $db->loadResultArray();
$query = "SELECT m_date FROM #__bl_match WHERE match_descr != '' ORDER BY m_date DESC";
$db->setQuery($query);
$videomatch2 = $db->loadResultArray();
PHP
$video = $this->videomatch;
for ($i=1;$i<=1;$i++){
echo '<div id="videoFrame" align="left"><iframe name="videoFrame" title="YouTube video
player" width="560" height="315" src="http://www.youtube.com/embed/' . $video[0] . '" frameborder="0" allowfullscreen=""></iframe></div>';
echo '<br />';
echo '<br />';
echo '<div id="videoThumbnails" align="left">';
echo '<table>';
foreach ($video as $value)
{
echo '<tr>';
echo '<td>';
echo '<img height="75px" width="123px" src="http://img.youtube.com/vi/' . $value. '/2.jpg" alt="' . $value . '" /> ';
echo '</td>';
echo '<td>';
echo (THIS IS WHERE I WOULD LIKE TO PUT THIS VIDEOS DATE);
echo '<br />';
echo 'California';
echo '<br />';
echo '1-19-2012';
echo '</td>';
echo '</tr>';
}
echo '</div>';}
You are (usually) better off selecting both rows in the same query:
SELECT match_descr, m_date FROM #__bl_match WHERE match_descr != '' ORDER BY m_date DESC
It appears that you are using Joomla. If this is the case, I don't think you can use loadResultArray() to iterate multiple columns (you can give it an index to get one of the columns, but that's not helpful in this case). Instead, you can use loadRowList to get an index array of the columns, or loadAssocList to get an associative array of the columns.
They can be used as follows (without all the html for brevity):
loadRowList:
$query = "SELECT match_descr, m_date FROM #__bl_match WHERE match_descr != '' ORDER BY m_date DESC";
$db->setQuery($query);
$videomatch = $db->loadRowList();
foreach ($videomatch as $row) {
echo "{$row[0]} ({$row[1]})\n";
}
loadAssocList:
$query = "SELECT match_descr, m_date FROM #__bl_match WHERE match_descr != '' ORDER BY m_date DESC";
$db->setQuery($query);
$videomatch = $db->loadAssocList();
foreach ($videomatch as $row) {
echo "{$row['match_desc']} ({$row['m_date']})\n";
}
Reference: http://docs.joomla.org/How_to_use_the_database_classes_in_your_script
I would like to suggest you following.
$query = "SELECT match_descr, m_date FROM #__bl_match WHERE match_descr != '' ORDER BY m_date DESC";
$db->setQuery($query);
$videomatch = $db->loadResultArray();
Your following loop only work one time. So I think you do not need a for loop here.
for ($i=1;$i<=1;$i++){
And you can change your foreach loop as bellow and use it
foreach ($videomatch as $video)
{
$videoCode = $video['match_descr'];
$date = $video['m_date'];
}
Please try. And if you want to work with $video[0] as you mentioned. Use it as $video[0]['match_descr'] .