Combine 2 arrays - php

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'] .

Related

Show the total amount of a column

I'm currently making a game to teach myself PHP as I go. I've already progressed quite a bit but I'm stuck now at the statistics where I want to show how many of each player-class there currently is in the game.
Thanks everyone, finally managed to fix it - not in the way as answered but the answer did help me getting to a result!
Which is:
<?php
echo '<b>Current Classes in the Game :</b>' . '<br />';
$result = $db->query("SELECT COUNT(*) FROM users WHERE role='Warrior'");
$row = $result->fetch_row();
echo $row[0] . ' Warriors' . '<br />';
$result = $db->query("SELECT COUNT(*) FROM users WHERE role='Mage'");
$row = $result->fetch_row();
echo $row[0] . ' Mages' . '<br />';
$result = $db->query("SELECT COUNT(*) FROM users WHERE role='Priest'");
$row = $result->fetch_row();
echo $row[0] . ' Priests' . '<br />';
$result = $db->query("SELECT COUNT(*) FROM users WHERE role='Rogue'");
$row = $result->fetch_row();
echo $row[0] . ' Rogues' . '<br />';
?>
You need execute an sql query like this using mysqli :
select
count(*) as nb,
class
from
yourTable
group by class

Output of DISTINCT and <> from SQL Query issue

I am obtaining some values from an array and making a match against these values in an SQL Query.
The code for this is as follows:
foreach($files as $ex){
$search = substr($ex,3,4);
echo $search . '<br>';
echo '<br>';
$sql = 'SELECT DISTINCT `pdb_code` FROM pdb WHERE `pdb_code` <> "' . $search . '" LIMIT 4';
}
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo 'SQL' . $row['pdb_code'] .'<br>';
$pdb[] = $row['pdb_code'];
}
The issue that I am having is that the <> seems not to be working.. I have even tried using the != operator, but still having the same issue.
The output of $search from the array are :
101m
102l
102m
103l
The output of SQL from the query is still:
101m
102l
102m
103l
Your code doesn't seem that logical, as you generate numerous SQL statements and then just execute the last one.
However I assume what you want to do is take a list of files, extract a string from each file name and then list all the pdb_code values from the table which are not already in the string.
If so something like this would do it. It takes each file name, extracts the sub string and escapes it, putting the result into an array. Then it builds one query, imploding the array to use in a NOT IN clause:-
<?php
$search_array = array();
foreach($files as $ex)
{
$search = substr($ex,3,4);
echo $search . '<br>';
echo '<br>';
$search_array[] = mysql_real_escape_string($search);
}
if (count($search_array) > 0)
{
$sql = "SELECT DISTINCT `pdb_code` FROM pdb WHERE `pdb_code` NOT IN ('" . implode("','", $search_array) . "') LIMIT 4";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo 'SQL' . $row['pdb_code'] .'<br>';
$pdb[] = $row['pdb_code'];
}
}
You have to use not in:
SELECT * FROM table_name WHERE column_name NOT IN(value1, value2...)
Try this:
$searchIds = implode(',',$search);
$sql = "SELECT DISTINCT `pdb_code` FROM pdb WHERE `pdb_code` NOT IN ('$searchIds') LIMIT 4";

array as column, shows nothing

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.

result of one mysql query in another query using php

i have a mysql query joining two tables, putting results in associative array using mysql_fetch_array and displaying 'name of cities' using while loop. In the same query, I need to do one more query using each corresponding 'city name' of associative array and show corresponding tags of each city.
$result = mysql_query("select city.city_ID, city.city_name, city.city_page, city_images.cimage_path from city, city_images where
city.city_ID=city_images.city_ID");
while ($row = mysql_fetch_array($result)) {
echo '<div class="Box">';
echo '<a href="' . $row['city_page'] . '">';
echo '<img src=" ' . $row['cimage_path'] . ' " />';
echo '</a>';
echo '<div class="imagetext">';
echo '' . $row['city_name'] . '';
echo '</div>';
echo '<div class="Inbox">';
$r = mysql_query("select Tag_Name from tag, city_tag where city_tag.city_ID =$row ['city_ID'] and city_tag.Tag_ID=tag.Tag_ID ");
while ($raw = mysql_fetch_array($r)) {
echo '<div class="boxtags">';
echo '</div>';
}
echo '</div>';
echo '</div>';
}
I need tags corresponding to current city..pls help
You only need one query...
SELECT c.city_ID
, c.city_name
, c.city_page
, ci.cimage_path
, t.Tag_Name
FROM city c
JOIN city_images ci
ON ci.city_ID = c.city_ID
JOIN city_tag ct
ON ct.city_ID = c.city_ID
JOIN tag t
ON tag.Tag_ID = ct.Tag_ID;

PHP MySQL Display counts

I have this query for counting the number of items in a category:
SELECT category, COUNT(*) AS category_count FROM users GROUP BY category
Which creates results looking like:
category category_count
========== ================
X 3
Y 2
Now, In PHP I want to display the counts of the categories. For example, I might want to echo the count from category X, how would I do it?
Thanks in advance
Assuming $result holds the result of your query:
while ($row = mysql_fetch_array($result))
{
echo 'Category: ' . $row['category'];
if ($row['category'] == 'X')
{
echo ' Count: ' . $row['category_count'];
}
echo '<br/>';
}
$res = mysql_query("SELECT category, COUNT(*) AS category_count FROM users GROUP BY category");
while($row = mysql_fetch_assoc($res)){
echo $row['category'].": ".$row['category_count']."<br/>";
}
$result = mysql_query("SELECT category, COUNT(*) AS category_count FROM users GROUP BY category");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
{
if ( $row['category'] == 'x' )
{
echo $row['category_count'];
}
}
while ($row = mysql_fetch_array($result))
{
echo 'Category: ' . $row['category'] . ' Count:' . $row['category_count'];
echo "<br>";
}
It would be better if you use the where clause in your query.
SELECT COUNT(*) AS category_count FROM users WHERE category = 'x'
$conn = mysql_connect("address","login","pas s");
mysql_select_db("database", $conn);
$Var = mysql_query("query");
While ($row = mysql_fetch_assoc($var) { echo $var["column"] }.

Categories