How to display foreign key values in single query? - php

I have two tables one is image and another one is tags.
image_id=primary key(IMAGE TABLE),fk_image_id=foreign key(TAGS TABLE).
Expected output: if example tagname two, image one to display single image and tagname (more names).
But now I get suppose two tagname, get two image separate ..how to solve this?
I using implode function but its coming " invalid arguments passed".
include_once("config.php");
$result=mysqli_query($mysqli,"SELECT * FROM image,tags WHERE image_id=fk_image_id ORDER BY creation_dt DESC LIMIT 5 ");
while($res = mysqli_fetch_array($result)) {
$tagname=$res['tag_txt'];
echo $tagname;
echo "<tr>"."<img src='http://localhost:8080/memes/".$res['path_txt']."' width='380' height='280' style='padding: 10px;' />"."</tr>";
}

Related

Print a database table and colour the parent row basing on its child colour

A PHP page must show the content of a database's table. In another table, there is the value I will use to print the row accordingly and show as a child.
$result = pg_query($conn, "SELECT *,'SENT' as direction FROM table1 WHERE identification LIKE '%$identification%' AND expedition LIKE '$expedition' order by date DESC LIMIT '$limit' ");
This, inside a WHILE, will print this kind of table:
while ($row = pg_fetch_row($result)) {
$sql2 = pg_query($conn, " SELECT quantity FROM expedition WHERE identification='$row[2]' ");
$row2 = pg_fetch_row($sql2);
$color="white";
if ($row2[0] == "-1") { $color="red";}
if ($row2[0] == "1") { $color="green";}
if ($row2[0] == "0") { $color="grey";}
echo "<tr bgcolor=\"$color\">
<td><button class=\"btn btn-info\" type=\"button\" data-toggle=\"collapse\" data-target=\"#$row[2]\">+</button></td>
<td>row[0]</td>
<td>row[1]</td>
<td>row[2]</td>
<td>row[3]</td>
<td>row[4]</td>
<td>...and more</td>
</tr>";
echo "<tr><td colspan=9><div style=\"background-color:$color\" id=\"$row[2]\" class=\"collapse\">$row2[0]</div></td></tr> ";
I am using bootstrap to add a button that, once clicked, will open a hidden row with the content of $sql2. Query that I will print straight after the </tr>.
Notice the 1st query as row[] and the second query as row2[].
The problem:
If the second query contains more than 1 result and in all cases identification is not a PK (therefore there could be more), I am not able to print for each row the colour of its childs.
I need to extend the second query to manage a WHILE loop, in order to extract, through identification, all the values of its parent.
IF, sql2 contains -1 or 1 or 0, the parent and itself must be painted accordingly.
IF, sql2 contains different values, both of them must be painted red (as error).
The obvious choice would be to move the SQL2 at the end of the first WHILE, in this case I can have SQL2 with all the records I want.
just like using arrays or the FOR structure.
Stays though the fact that moving the code would not allow me to print all the rows, because html is wrote already.
In this case I believe I'd need Jquery to highlight the values once they are written already.
I do not know a thing of Jquery and I'd appreciate any suggestion or help.
Attached a image to show what I achieved, the green bar only contain 1 child as the code above. Forgive the ugly interface!

Displaying Multiple rows (WHILE) ICONS

I have two while loops one is to loop through a chat log to retrieve date , username , message and the other while loop is to retrieve icons from a separate table this has two columns chars and image (image-name.*) I can display everything from the table chat but cannot seem to get my second while loop that contains the str_replace to loop through each row in the emo_cons table that may contain values , It only displays the last row in the table if I add a new chars value and image-name.* it shows me back that last one
// select all from table emo_cons that contain character values and image location
$emocom = mysqli_query($con,"SELECT * FROM `emo_cons`");
// fetch rows from query
while($emo_row= mysqli_fetch_assoc($emocom))
{
// assign chars row characters that represent the coresponding image
$chars = $emo_row['chars'];
// assign imagetag to row images that represent the coresponding characters
$imagetag = "<img width='50' class=image height='50' src='chaticons/".$emo_row['image']."' />";
echo " ";
echo "<br>";
} // end while emo_car check
$chat_log = mysqli_query($con,"SELECT * FROM chat ORDER BY id DESC");
// fetch all rows that contain characters and image locations
while($chat_row = $chat_log ->fetch_array()){ // i WANT THIS WHILE LOOP TO OUTPUT ALL ROWS THAT CONTAIN CHARS & IMAGES-LOCATIONS
// name of user in chat log
$username = $chat_row['username']; // this line is for usrer
echo "<br>";
// timestamp
echo $chat_row['date'];
echo "<br>"; // line break
//users profiler avatar image from chat_row while
echo "<img width='50' class=image height='50' src='avatars/".$chat_row['imagelocation']."' alt='Profile Pic'>";
echo " "; // space
// THIS LINE ONLY OUTPUTS THE LAST ROW OF AN ICON IN THE TABLE
// I would like my while to output all rows that contain str_replace chars TO imagetag from its table AND HAVE IT THEN PARSE TO THE MESSAGE [msg]
echo $username. " Says ".$new_str = str_replace($chars,$imagetag,$chat_row['msg']);
echo "<br>";
} // end while chat_log
echo '</div>'; // end div
?>
I need help understanding what I doing wrong I have tried moving my loops around to see if I had incorrectly structured them but had no luck so far
I just want to be able to display the chat message and if the message contains a character to replace that with an icon and do this always and NOT just for last row in my table .
I understand I may not get any help on this but its worth a mention maybe someone can see my errors . Thanks in advance .
UPDATE WORKING
Make following changes in your emo_cons while loop
<?php
// select all from table emo_cons that contain character values and image location
$emocom = mysqli_query($con,"SELECT * FROM `emo_cons`");
// fetch rows from query
$chars = array();
$imagetag = array();
while($emo_row= mysqli_fetch_assoc($emocom))
{
// assign chars row characters that represent the coresponding image
array_push($chars, $emo_row['chars']);
// assign imagetag to row images that represent the coresponding characters
array_push($chars, "<img width='50' class=image height='50' src='chaticons/".$emo_row['image']."' />");
echo " ";
echo "<br>";
} // end while emo_car check
?>
Let me know if it works for you.
Why your code was not working
Both the variables $chars and $imagetag were inside while loop and with every iteration values were overwriting. Hence the last value was stored in both the variables.
So to fix this, I created 2 arrays with same name and all the required values pushed into the arrays, which we later used in the str_replace function.

Line 70, Column 191: Duplicate ID recipeslide. Is there a workaround?

I'm getting three multiple errors when trying to validate my code.
echo '<section id="featured">';
$recipes = mysql_query("
SELECT `id`,`name`, `image`, `description`
FROM `recipe`
ORDER BY RAND() LIMIT 4;
");
while ($recipe = mysql_fetch_assoc($recipes)) {
echo '<section id="recipeslide">';
$recipe_id = $recipe['id'];
echo "<a href='php/recipe.php?id=$recipe_id'><img src=\"{$recipe['image']}\" height=100 width=100 /></a><br />";
echo '</section>';
}
echo '</section>';
This is the only place that I use the id of recipeslide but I think the validator is getting confused somehow and was wondering how I can fix this or do I ignore it?
It points out two other Id's that supposedly are duplicated.
It is also complaining about alt tags but I don't see that they would be effective in this situation.
IDs must be unique. You are using the same ID in a loop. You could do something like:
$count = 1;
while ($recipe = mysql_fetch_assoc($recipes)) {
echo '<section id="recipeslide' . $count++ . '">';
As for the alt attribute for <img>, it's pretty much required even if it's empty.
You are giving an id in a loop
So you get an extra ID each time round the loop
if you have 3 recipes then the while loop runs 3 times with each insertion adding another
<section id="recipeslide">
The answer is to not set a static id on an element inside of a loop, you need to either use a unique id for each run through the loop or if it is just for styling, set a class rather than an id

Indexing alphabetically ordered table with A-Z as "links"

I have a rather complicated question but perhaps you already understand what I am trying to accomplish by reading my title, hopefully.
I have a big list stored in SQLite and I am outputting the table into my webpage using php looping, but the problem that still remains is how to add index per letter.
For instance you have a list of items, each of which range from A all the way to Z
I only want to display every letter at one time, so if A comes first then only A should by default be visible
the rest should be accessed using a tab or link system with indexing (A - Z)
feedback please!
$stmt = $db->prepare('SELECT Title, Description, Alternative FROM TblOne;');
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "<table>";
echo "<thead><tr><th>Title</th><th>Description</th><th>Alternative</th></tr></thead>";
foreach($res AS $val) {
echo "<tr>";
foreach($val AS $val1) {
echo "<td><a href='.html'>$val1</a></td>";
}
echo "</tr>";
}
echo "</table>";
If you want to get the letters that start the field, use:
select distinct left(title, 1)
from tblOne
If you want to get the list that starts with a given letter:
SELECT Title, Description, Alternative
FROM TblOne
where left(title, 1) = LETTERYOUWANT

How can I display this table correctly?

I am working on a very simple webshop, it should echo different products in a table of 2 <td> by 4 <tr> but now it only displays the different products downwards. Hopefully someone here can help me.
$result=mysql_query("select * from products");
while($row=mysql_fetch_array($result)) {
$artikel = '<div style="background-color:#E3E3E3;width:200px;height:200px;"><img style="padding-top:10px;padding-left:25px;width:150px;height:150px;" src="'.htmlspecialchars($row['picture']).'"><br><div align="center"><b>'.htmlspecialchars($row['name']).'</b><br><h6>€'.htmlspecialchars($row['price']).'</h6></div></div>';
echo '<table><tr><td>'.$artikel.'</td>';
echo '</table>';
}
Why would it display a table of 2 by 4?
You are putting every product in its own table, so the first thing to do, is move the table tags out of the loop and then you need to add logic to add a new row after every x products.
Although you don't seem to need a table as your div has fixed dimensions, so you can just get rid of the table and use css to get the grid you want.
If this is your entire code then you're missing a tr tag.
Could be a possible cause for your lay-out shifting.
Instead of using breaks, I'd recommend using multiple divs (or table lay-out if you're more comfortable with that) as breaks aren't always handled the same in all browsers.
Instead of creating new table for every records, you can use one row per record.
$result=mysql_query("select * from products");
if(mysql_num_rows($result) > 0){
echo '<table>';
while($row=mysql_fetch_array($result)) {
$artikel = '<div style="background-color:#E3E3E3;width:200px;height:200px;"><img style="padding-top:10px;padding-left:25px;width:150px;height:150px;" src="'.htmlspecialchars($row['picture']).'"><br><div align="center"><b>'.htmlspecialchars($row['name']).'</b><br><h6>€'.htmlspecialchars($row['price']).'</h6></div></div>';
echo '<tr><td>'.$artikel.'</td></tr>';
}
echo '</table>';
}

Categories