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.
Related
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>";
}
I've written a PHP script that can populate a table in a particular way so that multiple events (or no events) can be put in one square in an HTML - similar to the layout a calendar would have. But, there's a problem, the while statement I created to fill in squares in the table when there is no data doesn't detect when there is data, and fills the entire table with empty squares. This is what the output looks like (The page is styled using Bootstrap 3). From the mysql data I have provided, these events should be in the square at {Period 1, Monday}.
Here is my data in a mysql database; mysql data
Here is a snippet of the part of the page related to this table;
<?php
$query = "SELECT * FROM configtimetabletwo WHERE term = ".$term." AND week = ".$week." ORDER BY period, day LIMIT 100;";
$results = mysqli_query($conn, $query);
$pp=1; //The current y value of the table
$pd=0; //The current x value of the table
echo '<tr><td>';
while($row = mysqli_fetch_row($results)) {
while((pd!=$row[3] or $pp!=$row[4]) and $pp<6){ //This while statement fills in empty squares and numbers each row.
if($pd==0) {
echo $pp."</td><td>";
$pd++;
}
elseif($pd<5){
echo "</td><td>";
$pd++;
}
else {
echo "</td></tr><tr><td>";
$pd=0;
$pp++;
}
}
echo '<a href="?edit='.$row[0].'" class="label label-default">';
echo $row[5].' '.$row[6].' - '.$row[7]."</a><br>";
}
echo "</td></tr></table>"
?>
I haven't been able to figure out why this happens so far, thanks in advance to anyone who has any idea what's going on.
In the comments below my question, pavlovich pointed out the error. In this case, it was simply an issue of forgetting to use a $ to reference a variable. It would seem that this doesn't throw an error in a while statement like it would elsewhere.
I have a procedure that outputs a table with 4 columns and 2 rows. I want data on the 3rd column and second row. The code here gives me the row data for the different columns as i change them below but it outputs the first and second row data for the 3rd column like this: row1datarow2data. Its both row values and I only want the 2nd row. Can someone please help me fix this.
Thanks.
<?php
$result=odbc_exec($conn, $query);
//fetch tha data from the database
while(odbc_fetch_row($result))
{
echo "<tr>";
echo "<td>";
//if i change the 3 under here to a 2 I get the other two row values for the 2nd,column.
echo odbc_result($result,3);
?>
The code as you have shown it is going to loop through all the records and echo the column value specified in your call to odbc_result().
If you do not want to echo the value from the first row, your code needs to detect which row of data is currently being fetched and only echo the column value for the desired row.
For example, in pseudo code:
// include inside of fetch loop
if (current_row is row2){
echo 3rd_column_data_value;
}
All the whole day I'm trying to solve this problem but still no luck.
The scenario is: I am developing a vertical menu which should query groups and items of those groups in a menu respectively, but groups are being populated without its items.
Code was written in the following way:
$data1 = mysql_query(select groupnames from groups where categoryy='Agriculture');
while($info1=mysql_fetch_array($data1))
{
echo $info1[0];
$data2==mysql_query(select itms from groupitems where categoryy='Agriculture' and groupname='$info1[0]');
while($info2=mysql_fetch_array($data2))
{
echo $info2[0];
}
}
In the above code, groups are being populated nicely but no items from groupitems table are being populated. If I write Grain (Grain is one of the group of agriculture in my database) instead of groupname=$info1[0] then it works. But it should be got dynamically from the query.
Please help, I'm in trouble.
at last its solved! here's the code:
<?php
include "aPannel/dbconn.php";
$query="select GroupName from categorygroup where categoryy='Agriculture'";
$i=0;
$result=mysql_query($query);
$num=mysql_num_rows($result);
$groupname=mysql_result($result ,$i ,"GroupName");
mysql_close();
if ($num=="0") echo "<p>Sorry, there are no groups to display for this Category.</p>";
else
{
echo "<p>There are currently <strong>$num</strong> groups represented in our database.</p><br>";
while($i < $num)
{
$groupname=mysql_result($result ,$i ,"GroupName");
include("aPannel/dbconn.php");
$query2 = "SELECT subcategory FROM groupsubcategory WHERE groupname='$groupname'"; // count number of items in each group
echo $query2 . "<br/>";
$resultt=mysql_query($query2);
$countt=mysql_num_rows($resultt);
mysql_close();
echo $countt . "subcategories" . "<br/>"; // display number of items
$i++;
}
} // end if results
?>
Your queries are not wrapped around double-quotes (" ") . Always remember that what you pass to mysql_query method is a string argument. And also $data2==.... seems wrong.
So, change the code like this
$data1=mysql_query("select groupnames from groups where categoryy='Agriculture'");
while($info1=mysql_fetch_array($data1))
{
echo $info1[0];
$infoTemp=$info1[0];
$data2=mysql_query("select itms from groupitems where categoryy='Agriculture'
and groupname='$infoTemp'");
while($info2=mysql_fetch_array($data2))
{
echo $info2[0];
}
}
I hope it should work
EDIT: Also are you sure column itms in second query or items ?
EDIT: added temporary variable
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