Joining two different tables containing csv and display them on a webpage - php

I have been stuck with a problem and I am newbie in mysql and php, Here is the code first , so that I can explain in detail:
$metros = array(1,263);
foreach($metros as $metro_id) {
$sql = "SELECT cuisine_id, cuisine_name_en FROM poi_restaurant_cuisines";
$result = mysql_query($sql);
$cuisine_id = array();
$cusine_name = array();
while($row = mysql_fetch_assoc($result)) {
$cuisine_id[] = $row['cuisine_id'];
$cuisine_name[] = $row['cuisine_name_en'];
}
foreach ($cuisine_id as $cuisine) {
$sql = "
SELECT COUNT(*)
FROM poi AS p
LEFT JOIN poi_restaurant AS pr USING (poi_id)
WHERE p.poi_address_prefecture_id = '$metro_id'
AND pr.poi_restaurant_cuisine_id_array
AND find_in_set('$cuisine', poi_restaurant_cuisine_id_array)
AND p.poi_status = 1";
$result = mysql_query($sql);
$count_cuisine = array();
while($row = mysql_fetch_array($result)) {
$count_cuisine[$metro_id][$cuisine] = $row['COUNT(*)'];
}
echo "<table border = 1 cellpadding= 5 cellspacing= 5 width= 100>";
echo "<tr><th>CuisineID</th><th>Count</th></tr>";
echo "<tr><td>";
echo $cuisine;
echo "</td><td>";
echo $count_cuisine[$metro_id][$cuisine];
echo "</td><td>";
echo "</tr>";
echo "</table>";
}
}
The poi_restaurant_cuisine_id_array contains csv values. I am able to produce the count and the cuisine ID on the web page. I want to replace the cuisine ID with the name of the cuisine. I am not very good at sql or either PHP as I am a beginner. I hope I am being clear enough. Any help is highly appreciated ...Thank you.

Try this:
echo '<table border="1" cellpadding="5" cellspacing="5" width="100">';
echo "<tr><th>Cuisine</th><th>Count</th></tr>";
$metros = array(1,263);
foreach($metros as $metro_id) {
$sql = "SELECT cuisine_id, cuisine_name_en FROM poi_restaurant_cuisines";
$result = mysql_query($sql);
$cuisines = array();
while($row = mysql_fetch_assoc($result)) {
$cuisines[] = array(
'id' => $row['cuisine_id'],
'name' => $row['cuisine_name_en'],
);
}
foreach ($cuisines as $cuisine) {
$sql = "
SELECT COUNT(*)
FROM poi AS p
LEFT JOIN poi_restaurant AS pr USING (poi_id)
WHERE p.poi_address_prefecture_id = '$metro_id'
AND pr.poi_restaurant_cuisine_id_array
AND find_in_set('{$cuisine['id']}', poi_restaurant_cuisine_id_array)
AND p.poi_status = 1";
$result = mysql_query($sql);
$count_cuisine = array();
while($row = mysql_fetch_array($result)) {
$count_cuisine[$metro_id][$cuisine['id']] = $row['COUNT(*)'];
}
echo "<tr>
<td>{$cuisine['name']}</td>
<td>{$count_cuisine[$metro_id][$cuisine['id']]}</td>";
</tr>";
}
}
echo "</table>";

Assuming cuisine_id is a unique identifier, then just use it as the array index....
while($row = mysql_fetch_assoc($result)) {
$cuisines[$row['cuisine_id']] = $row['cuisine_name_en'];
}
....
foreach ($cuisines as $cuisine_id=>$cuisine_name_en) {
However storing multiple values in a single column is a very bad idea.
Generating and running queries in a loop is another very bad idea.
It is possible to reduce this to a single query, declared and invloked outside the inner loop but because your data is not mormalized, this is rather complex.

Related

display multiple rows from oracle database with php

I am trying to display multiple rows from Oracle table with php.
Below is my code:
<?php
include("dbconnect.php");
$personcon=$conn;
$surname = $_POST['Surname'];
$suburb = $_POST['Suburb'];
$state = $_POST['State'];
$discipline = $_POST['Discipline'];
$companyname = $_POST['CompanyName'];
$rank = $_POST['Rank'];
$role = $_POST['Role'];
$yearsexperience = $_POST['YearsExperience'];
$recentproject = $_POST['RecentProject'];
$sellrate = $_POST['SellRate'];
$projectnumber = $_POST['ProjectNumber'];
$tendernumber = $_POST['TenderNumber'];
$query = "SELECT CP.ROLE AS CURRENT_ROLE , CP.SURNAME, CP.FIRSTNAME, CP.COMPANY,CC.COMPANYNAME AS CONSULTANT_COMPANY, CP.YEARSEXPERIENCE AS EXPERIENCE_IN_YEAR, CP.QUALIFICATION, D.PRINCIPLEDISCIPLINE, D.SUBDISCIPLINE1, D.SUBDISCIPLINE2, D.SUBDISCIPLINE3, D.SUBDISCIPLINE4, D.SUBDISCIPLINE5, D.SUBDISCIPLINE6, D.SUBDISCIPLINE7, D.SUBDISCIPLINE8, L.SUBURB, L.STATE, L.COUNTRY FROM CONSULTANTPERSONNEL CP, LOCATION L, ONSULTANTPERSONNEL_DISCIPLINE N, DISCIPLINE D, CONSULTANTCOMPANY CC, CONTRACTS C, PROJECTS P, TENDERS T, FEEBASIS F WHERE CP.LOCATIONID = L.LOCATIONID AND CP.CONSULTANTPERSONALID = N.CONSULTANTPERSONALID AND N.DISCIPLINID = D.DISCIPLINID AND CP.CONSULTANTCOMPANYID = CC.CONSULTANTCOMPANYID AND CP.CONTRACTID = C.CONTRACTID AND C.FEECODE = F.FEECODES AND C.PROJECTNUMBER = P.PROJECTNUMBER AND C.TENDERNUMBER = T.TENDERNUMBER AND LOWER(CP.SURNAME) LIKE LOWER('%".$surname."%') AND LOWER(L.SUBURB) LIKE LOWER('%".$suburb."%') AND LOWER(L.STATE) LIKE LOWER('%".$state."%') AND LOWER(D.PRINCIPLEDISCIPLINE) LIKE LOWER('%".$discipline."%') AND LOWER(CC.COMPANYNAME) LIKE LOWER('%".$companyname."%') AND LOWER(CP.RANK) LIKE LOWER('%".$rank."%') AND LOWER(CP.ROLE) LIKE LOWER('%".$role."%') AND LOWER(P.PROJECTNAME) LIKE LOWER('%".$recentproject."%') AND LOWER(P.PROJECTNUMBER) LIKE LOWER('%".$projectnumber."%') AND LOWER(T.TENDERNUMBER) LIKE LOWER('%".$tendernumber."%')";
$SQL = oci_parse($personcon, $query);
oci_execute($SQL);
$results = array();
$numRows = oci_fetch_all($SQL, $results, null, null, OCI_FETCHSTATEMENT_BY_ROW);
if($numRows > 0){
echo "<p> <table border=1>\n";
//Print the headings
echo "<tr>\n";
foreach($results[0] as $index=>$value)
echo "<th>$index</th>\n";
echo "</tr>\n";
echo "<tr>\n";
foreach($results as $row)
foreach($row as $index=>$value)
echo "<td>$value</td>\n";
echo "</tr>\n";
echo "</table>\n </p>";
oci_close($personcon);
} else {
echo "<tr> The search you enter is not in the database.";
}
?>
I can retrieve the data I wish but instead of displaying row by row, it displays all the data in one row.
Any Idea how to fix that ? Thanks
You got simple and common (for me at least) mistake in Your loops, that creates only one row and puts every column inside. Try changing:
echo "<tr>\n";
foreach($results as $row)
foreach($row as $index=>$value)
echo "<td>$value</td>\n";
echo "</tr>\n";
to something like this:
foreach($results as $row)
{
echo "<tr>\n";
foreach($row as $index=>$value)
{
echo "<td>$value</td>\n";
}
echo "</tr>\n";
}
The brackets will help You see the scopes. Its not python, You know. ;)

Failed to iterate over selected rows

The code is supposed to get a row at each loop and build td elements for each data piece. However, it only gets some rows while missing others even though all rows were selected:
$query = "select * from category";
$ret = mysql_query($query, $conn);
while ($rows = mysql_fetch_assoc($ret)) {
echo "<tr>";
foreach ($rows as $val) {
echo "<td>{$val}</td>";
}
}
mysql_fetch_assoc returns an associative array. To display each returned row, do something like this (taken directly from the php.net page for mysql_fetch_assoc
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
Try removing the curly braces like this:
echo "<td>$val</td>";
Beside you didn't close the <tr> tag. So you should do this:
$query = "select * from category";
$ret = mysql_query($query, $conn);
while ($rows = mysql_fetch_assoc($ret)) {
echo "<tr>";
foreach ($rows as $val) {
echo "<td>$val</td>";
}
echo"</tr>";
}
Now like Jack Albright said you should consider various columns of your table to display specific data. Upon that the while() is already a look, I think you don't need to add any foreach() inside. SO your final code should look like this:
$query = "select * from category";
$ret = mysql_query($query, $conn);
while ($rows = mysql_fetch_assoc($ret)) {
echo "<tr>";
echo "<td>$rows['columnName']</td>";
echo "</tr>";
}

How to find in mysql_fetch_array while and foreach cycle variables?

I need to do a search in each $row2 variable, so i could find if there is columns that are not equal to $row1 column.
This is what I tried,
$result1 = mysql_query('SELECT gpu FROM ng2s3_content');
$result2 = mysql_query("SELECT gpu FROM bigc3_gpu");
while ($row1 = mysql_fetch_array($result1)) {
while ($row2 = mysql_fetch_array($result2)) {
foreach ($row2 as $ar) {
if ($ar == $row1[0]) {
}
else {
echo $ar;
}
}
}
}
looks like you want distinct values from your database ,
use mysql query , that will return a resource with distinct values ,
$result = mysql_query('SELECT gpu FROM ng2s3_content UNION SELECT gpu FROM bigc3_gpu');
while ($row = mysql_fetch_array($result)) {
echo $row['gpu'];
}

Why does this query show only one result?

The query I have below will only show me one result even if there are multiple matching entries (completely or partially matching). How do I fix it so it will return all matching entries:
//$allowed is a variable from database.
$sql = "SELECT `users`.`full_name`, `taglines`.`name`, `users`.`user_id` FROM
`users` LEFT JOIN `taglines` ON `users`.`user_id` = `taglines`.`person_id`
WHERE ( `users`.`user_settings` = '$allowed' ) and ( `users`.`full_name`
LIKE '%$q%' ) LIMIT $startrow, 15";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$num_rows1 = mysql_num_rows($result);
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$person = htmlspecialchars($row['full_name']);
}
}
}
print $person;
Because your overwriting $person on each iteration.
Hold it in a $person[] array if your expecting more then one. Then loop through it with a foreach loop when you intend to output.
Not related but your also querying twice, you only need 1 $result = mysql_query($sql);
Update (Simple Outputting Example):
<?php
$person=array();
while($row = mysql_fetch_array($query)){
$person[] = array('full_name'=>$row['full_name'],
'email'=>$row['email'],
'somthing_else1'=>$row['some_other_column']);
}
//Then when you want to output:
foreach($person as $value){
echo '<p>Name:'.htmlentities($value['full_name']).'</p>';
echo '<p>Eamil:'.htmlentities($value['email']).'</p>';
echo '<p>FooBar:'.htmlentities($value['somthing_else1']).'</p>';
}
?>
Or an alternative way to is to build your output within the loop using concatenation.
<?php
$person='';
while($row = mysql_fetch_array($query)){
$person .= '<p>Name:'.$row['full_name'].'</p>';
$person .= '<p>Email:'.$row['email'].'</p>';
}
echo $person;
?>
Or just echo it.
<?php
while($row = mysql_fetch_array($query)){
echo '<p>Name:'.$row['full_name'].'</p>';
echo '<p>Email:'.$row['email'].'</p>';
}
?>

PHP/MySQL Query Help

I have a query like this:
$query = "SELECT gyms.name FROM `fighters_team` INNER JOIN gyms ON gyms.id = fighters_team.gym_id WHERE fighter_id = $fighter_id";
if ($result = $mysqli->query($query)) {
while($row = $result->fetch_assoc()) {
echo $row['name'];
echo " / ";
}
}
What comes up is: "Team 1 / Team 2 /"
What I want to do is get rid of the trailing slash on the last item. So it should just say "Team 1 / Team 2" if there are two items.
Does anyone have any idea how I could approach that?
Thanks!
$query = "SELECT gyms.name FROM `fighters_team` INNER JOIN gyms ON gyms.id = fighters_team.gym_id WHERE fighter_id = $fighter_id";
$names = array();
if ($result = $mysqli->query($query)) {
while($row = $result->fetch_assoc()) {
array_push($names, $row['name']);
}
echo implode(' / ', $names);
}
I hope this would work.
Try:
$query = "SELECT gyms.name FROM `fighters_team` INNER JOIN gyms ON gyms.id = fighters_team.gym_id WHERE fighter_id = $fighter_id";
if ($result = $mysqli->query($query)) {
while($row = $result->fetch_assoc()) {
$teams .= $row['name'].' / '.'';
echo rtrim($teams,'/ ');
}
}
Is that what you are looking for?

Categories