How to exclude a certain name out of my table - php

I am running a gameserver and for my gameserver I made a table that shows up your items.
The table cleared it up, but the problem is that there is a item called "GMP" is spamming the list because people got it verry much.
What I want to do is excluding the item "GMP" out of that whole table but I couldn't get it out of the table.
http://puu.sh/nefH . This is how it looks like.
Here is the part of the source code to let it show this.
print <<<END
<br>
<table border="1">
<tr>
<th>Item</th>
<th>+</th>
<th>Damage -</th>
<th>Extra hp</th>
<th>Position</th>
</tr>
END;
$sql6 = "SELECT t.name, i.position, p.position, i.type, i.magic3, i.reduce_dmg, i.add_life FROM cq_item i, position p, cq_itemtype t WHERE t.id = i.type AND p.id = i.position AND player_id = $id2 ORDER BY name ASC";
$execute6 = mysql_query($sql6);
while ($exibir6 = mysql_fetch_array($execute6)){;
print "<tr>";
print "<td>".$exibir6 ['name']."</td>";
print "<td>".$exibir6 ['magic3']."</td>";
print "<td>".$exibir6 ['reduce_dmg']."</td>";
print "<td>".$exibir6 ['add_life']."</td>";
print "<td>".$exibir6 ['position']."</td>";
print "</tr>";
}
print <<<END2
</table>
END2;
If there is something else needed just tell me and I will add it to here.
Sorry for my English it isn't my native language.

Don't select items with that name (use WHERE for that):
$sql6 = "
SELECT t.name, i.position, p.position, i.type, i.magic3, i.reduce_dmg, i.add_life
FROM cq_item i, position p, cq_itemtype t
WHERE t.id = i.type AND p.id = i.position AND player_id = $id2 AND t.name != 'GMP'
ORDER BY name ASC";
nb. I hope you escape/cast your query variables properly

$sql6 = "SELECT t.name, i.position, p.position, i.type, i.magic3, i.reduce_dmg, i.add_life FROM cq_item i, position p, cq_itemtype t WHERE t.id = i.type AND p.id = i.position AND player_id = $id2 AND t.name != 'GMP' ORDER BY name ASC";

Do an if statement in your loop.
<?php
while ($exibir6 = mysql_fetch_array($execute6)){
if($exibir6['name'] != 'GMP') {
// Add to table
}
}
?>

Related

show 2 table in 1 using inner join query

Hey guys i want to show a table so that table have 2 foreign keys.My food and restaurant table have name column.i tried following codes but i dont know how to show restaurant name and food name different.
$sql_select_food = "SELECT food.id,restaurant.name,food.name,food.restaurant_id,foodcategory.title from food INNER join foodcategory ON food.foodcategory_id=foodcategory.id INNER join restaurant ON food.restaurant_id=restaurant.id WHERE food.isActive = 1 ORDER BY food.updateDate DESC";
$result_select_food = mysqli_query($connection, $sql_select_food);
if (mysqli_num_rows($result_select_food) > 0) {
while ($row_select_food = mysqli_fetch_assoc($result_select_food)) {
echo "
<tr>
<td>$row_select_food[name]</td>
<td>$row_select_food[name]</td>
<td>$row_select_food[title]</td>
</tr>
";
}
}
Use column aliases:
SELECT food.id AS food_id, restaurant.name AS restaurant_name, food.name AS food_name ...
Then
$row_select_food['restaurant_name'] is the name of the restaurant,
$row_select_food['food_name'] is the name of the food,
etc.
Note: AS keyword is optional.
Put an alias to the column name and call it later in the code as a column name like :
$sql_select_food = "SELECT food.id,restaurant.name as rname, food.name as fname, food.restaurant_id,foodcategory.title from food INNER join foodcategory ON food.foodcategory_id=foodcategory.id INNER join restaurant ON food.restaurant_id=restaurant.id WHERE food.isActive = 1 ORDER BY food.updateDate DESC";
$result_select_food = mysqli_query($connection, $sql_select_food);
if (mysqli_num_rows($result_select_food) > 0) {
while ($row_select_food = mysqli_fetch_assoc($result_select_food)) {
echo "
<tr>
<td>$row_select_food[rname]</td>
<td>$row_select_food[fname]</td>
<td>$row_select_food[title]</td>
</tr>
";
}
}

MySQL SELECT to subquery a many-to-many relationship

I have a bilingual dictionary database that I've created, and the tables are set up like so:
lemma (lemmaID, lemma, meaning)
collocate (collocateID, lemmaID, collocate, notes, connection)
collusage (usageID, lemmaID_u, collocateID_u, japanese, english, englishalt)
partofspeech (posID, partofspeech)
postolemma (lemmaID_p, posID_p)
So far, I have a query that returns tables for the results, and it works just how I'd like it to. (It looks like this)
$q = 'SELECT *
FROM lemma, collocates, collusage
WHERE lemma.lemmaID = collocates.lemmaID AND lemma.lemmaID = collusage.lemmaID_u AND collusage.collocateID_u = collocates.collocateID
ORDER BY lemma.lemmaID;';
$result = mysqli_query($con, $q) or die(mysql_error());
if (!$result || mysqli_num_rows($result) == 0) {
echo 'No rows found';
exit;
}
$lastCatID = 0;
while ($row = mysqli_fetch_assoc($result)) {
$reading = $row['reading'];
$headword = $row['lemma'];
$collocate = $row['collocate'];
if (isset($row['notes'])) {
$notes = '&lpar;'.$row['notes'].'&rpar;';
} else {
$notes = $row['notes'];
}
$japanese = $row['japanese'];
$english = $row['english'];
if (isset($row['englishalt'])) {
$englishalt = ', '.$row['englishalt'].'';
} else {
$englishalt = $row['englishalt'];
}
if ($lastCatID != $row['lemmaID']) {
//starting a new category
if ($lastCatID != 0) {
//close up previous table
echo ' </tbody>
</table> </div>';
}
//start a new div
echo '<div class="entry">
<h4>'.$reading.'【'.$headword.'】 <span class="pos">'.$WANT TO LIST PARTS OF SPEECH HERE.'</span></h4>
<table class="table table-striped table-hover">
<tbody>';
$lastCatID = $row['lemmaID'];
}
echo '<tr>
<td><span>'.$collocate.'</span><span class="notes">'.$notes.'</span></td>
<td>'.$japanese.'</td>
<td>'.$english.''.$englishalt.'</td>
</tr>';
}
if ($lastCatID != 0) {
//close up the final table
echo ' </tbody>
</table></div>';
}
mysqli_free_result($result);
What I can't figure out how to do is to use the postolemma junction table to get all of the partofspeech values for each lemmaID so I can list them next to the lemma in the table. Everything SELECT query I have done so far has duplicated collocation entries, which I don't want. Any help is appreciated!
Edit: Here is a link to the SQL Fiddle with data. I couldn't get my foreign key constraints to work so just that is missing.
if I understand you correctly you want to select all values from table partofspeech based on lemma table. Your query should look like this:
SELECT part.partofspeech
FROM partofspeech part
INNER JOIN postolemma post
ON part.posID = post.posID_p
INNER JOIN lemma l
ON post.lemmaID_p = l.lemmaID
Also I would suggest you to change the query you use and start using JOIN operator in syntax, it's good practice and it's not hard to switch from one to another... So your query:
SELECT *
FROM lemma, collocates, collusage
WHERE lemma.lemmaID = collocates.lemmaID
AND lemma.lemmaID = collusage.lemmaID_u
AND collusage.collocateID_u = collocates.collocateID
ORDER BY lemma.lemmaID;
Will look like this:
SELECT *
FROM lemma
INNER JOIN collocates
ON lemma.lemmaID = collocates.lemmaID
INNER JOIN collusage
ON collusage.collocateID_u = collocates.collocateID
AND lemma.lemmaID = collusage.lemmaID_u
ORDER BY lemma.lemmaID;
Also you can use aliases for table in you query like i do in first query I wrote here. It will make your life easier because you don't need to type whole name of table over and over...
GL!
P.S. also it's good to post your desired result in you question and provide SQL Fiddle with some data for our better understanding of your queston...
EDIT
After we consulted in the comments we come to this solution:
SELECT *
FROM lemma
INNER JOIN collocates
ON lemma.lemmaID = collocates.lemmaID
INNER JOIN collusage
ON collusage.collocateID_u = collocates.collocateID
AND lemma.lemmaID = collusage.lemmaID_u
INNER JOIN (SELECT post.lemmaID_p AS lemmaID, group_concat(part.partofspeech SEPARATOR ', ') AS partofspeach
FROM partofspeech part
INNER JOIN postolemma post
ON part.posID = post.posID_p
INNER JOIN lemma l
ON post.lemmaID_p = l.lemmaID
GROUP BY post.lemmaID_p) tmp
ON lemma.lemmaID = tmp.lemmaID
ORDER BY lemma.lemmaID;
Here is SQL Fiddle for that...

Why does one of my column shows empty?

I have my table that one of my column shows empty. It has the column of Id, Date, Cust name, Product + Qty, and amount. But only in Product + Qty shows empty even it has data in database.
PHP code
<?php
include('connect.php');
$start = isset($_GET['d1']) ? $_GET['d1'] : '';
$end = isset($_GET['d2']) ? $_GET['d2'] : '';
if(isset($_GET['submit']) && $_GET['submit']=='Search')
{
$result = mysql_query(
"SELECT
t1.qty,
t2.lastname,
t2.firstname,
t2.date,
t3.name,
t2.reservation_id,
t2.payable FROM prodinventory AS t1
INNER JOIN reservation AS t2
ON t1.confirmation=t2.confirmation
INNER JOIN products AS t3
ON t1.room=t3.id
WHERE str_to_date(t2.date, '%d/%m/%Y') BETWEEN
str_to_date('$start', '%d/%m/%Y') AND
str_to_date('$end', '%d/%m/%Y')
GROUP BY t2.confirmation") or die(mysql_error());
while ($row = mysql_fetch_array($result)){
echo'<tr class="record">';
echo '<td>'.$row['reservation_id'].'</td>';
echo '<td>'.$row['date'].'</td>';
echo '<td>'.$row['firstname'].' '.$row['lastname'].'</td>';
echo '<td><div align="left">';
$rrr=$row['confirmation'];
$results = mysql_query("SELECT * FROM prodinventory where confirmation='$rrr'");
while($row1 = mysql_fetch_array($results))
{
$roomid=$row1['room'];
$resulta = mysql_query("SELECT * FROM products where id='$roomid'");
while($rowa = mysql_fetch_array($resulta))
{
echo $rowa['name'].' x';
}
echo ' '.$row1['qty'].'<br>';
}
echo '<td>'.'PHP ' . number_format(floatval($row['payable']));
}
?>
Hmmmm I have deleted my answer but noone tried so...
I think this echo ' '.$row1['qty'].'<br>'; is the row you asked about. And all this looks like a typo. If this is the case:
You have no confirmation in the SELECT clause (it's used only in JOIN and GROUP BY) and it possible your $rrr to be blank. Echo it to be sure there is a value.
Check does your query works and return results. Echo the query string (or take it from the mysql log file) and test it.
You have SELECT *. Is the field name 'qty' correct in a case-sensivity environment? 'Qty' may be different and the query may work but you don't get the result.
i think that's because you have inner join and maybe the intersection tables have no data
try to do left join first if it works
insure that all of tables have data in it

How do I only print categories that belong to the current post? This snippet shows all the categories in the site

I have this snippet that shows all taxonomy list in the site, that belongs to a specific vocabulary.
Instead of printing the whole list, how do I just print the terms that belong to the node I'm actually loading?
I have a Drupal 7 installation.
This is how I print the id of the node I´m at: <?php print $node->nid;?>
<?php
$vid = 11; //vocabulary id
$query = "SELECT tid, name, count
FROM (
SELECT td.tid AS tid, name, COUNT(td.tid) AS count
FROM taxonomy_term_data AS td
JOIN taxonomy_index AS tn
ON td.tid = tn.tid
JOIN node AS n
ON n.nid = tn.nid
WHERE td.vid = ". $vid ."
AND n.status = 1
GROUP BY td.tid
ORDER BY count DESC
) AS t
ORDER BY name ASC";
$result = db_query($query);
foreach($result as $term) {
if ($term->count > 0) {
echo l($term->name, "taxonomy/term/$term->tid").' ('.$term->count.')'.'<br/>';
}
}
?>
I would suggest not to run extra query for this.
This information should be available in $node object.
Just print it [print_r($node) ] and see what exactly is the taxonomy object name($node->taxonomy) & how taxonomy information is structured & use that to display category on node page or node teaser.
On other pages, you can use node_load to 1st load the node and then do the same thing.
sumoand's answer is more optimal in this case, however for some sql practicing here's the exact solution the way you imagined:
<?php
$vid = 11; //vocabulary id
$query = "SELECT tid, name, count
FROM (
SELECT td.tid AS tid, name, COUNT(td.tid) AS count
FROM taxonomy_term_data AS td
JOIN taxonomy_index AS tn
ON td.tid = tn.tid
JOIN node AS n
ON n.nid = tn.nid
WHERE td.vid = ". $vid ."
AND n.status = 1
AND n.nid = ".$node->id."
GROUP BY td.tid
ORDER BY count DESC
) AS t
ORDER BY name ASC";
$result = db_query($query);
foreach($result as $term) {
if ($term->count > 0) {
echo l($term->name, "taxonomy/term/$term->tid").' ('.$term->count.')'.'<br/>';
}
}
?>

PHP MYSQL array

I'm running the following code in PHP to generate the random list of movies from the database but since value of genere is an array it is just capturing the first value of the array.
How can I modify the query so that I can get all the values of genere in comma separated line.
$q = "SELECT *
FROM
title
INNER JOIN title_genere ON (title.id = title_genere.id_title)
INNER JOIN genere ON (title_genere.id_genere = genere.id)
ORDER BY RAND()
LIMIT 8";
$result = mysql_query($q);
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$title = $row['title'];
$year = $row['year'];
$poster = $row['poster'];
$poster = str_replace("./", "lib/", $poster);
$genere = $row['genere'];
echo "<div id='a'>";
echo "<div id='b'>".$title.'</div>';
echo "<div id='c'>".$year.'</div>';
echo "<div id='d'><a href='select.php?movieid=$id'><img src='$poster' alt='' border='1' align='center' width='214' height='314' /></a></div>";
echo "<div id='e'>".$genere.'</div>';
echo "</div>";
//var_dump($genere);
}
Try this one:
$q = "SELECT
*,
GROUP_CONCAT(CAST(title_genere AS BINARY) SEPARATOR ',') AS generes
FROM
title
INNER JOIN title_genere ON (title.id = title_genere.id_title)
INNER JOIN genere ON (title_genere.id_genere = genere.id)
ORDER BY RAND()
GROUP BY title.id
LIMIT 8";
This should select a comma-separated list on generes into the key generes. For more information see the manual at http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
Don't use GROUP_CONCAT/GROUP BY, it causes overhead.
First, select your 8 random titles (from the titles table only) into an associative array indexed by title id. Let's name it $idx.
Next, prepare the id list, say, as $ids variable.
Then, run the query
SELECT
title_genere.id_title
, genre.genre
FROM
title_genre
INNER JOIN genre ON title_genere.id_genere = genere.id
WHERE
title_genere.id_title IN ($ids)
ORDER BY
genre
and harvest genre values in lists in $idx:
$idx[$row['id_title']]['genres'][] = $row['genre']
Finally, generate HTML from $idx.
Sorted out. I use below code to generate what I want. Thanks everyone for the help
SELECT
title.id,
title.title,
GROUP_CONCAT(genere SEPARATOR ' | ') AS genere,
title.`year`,
title.poster
FROM
title
INNER JOIN title_genere ON (title.id = title_genere.id_title)
INNER JOIN genere ON (title_genere.id_genere = genere.id)
GROUP BY
title.id,
title.title,
title.`year`,
title.poster
ORDER BY
RAND()
LIMIT 8

Categories