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
Related
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.
I have a database that holds thousands of structures. The structures are searchable by choosing the "area" first, then selecting the "block_number". My first page allows the user to select the area, the area is then passed through the url to the next page. The next page uses php to pull up the blocks in that area. I'm trying to echo the "area" and "block_number" in the results. The my query works just fine but, for some reason I can't display the "area" in the results. See the code below.
<?
include("conn.php");
include("pl_header.php");
$area = mysql_real_escape_string($_GET['area']);
$wtf = '$area';
?>
<h3>Choose A Block Number in<br> <?=$area?></h3><br>
<center>
<?php
$tblWidth = 1;
$sql = mysql_query("SELECT DISTINCT block_number FROM platform_locations WHERE area='$area'");
$i = 1;
// Check to see if any results were returned
if(mysql_num_rows($sql) > 0){
echo '<div class="redBox extraIndent">';
// Loop through the results
while($row = mysql_fetch_array($sql)){
echo ''. $row['area'] .''. $row['block_number'] .'';
if($i == $tblWidth){
echo '';
$i = 0;
}
$i++;
}
echo '';
}else{
echo '<br>Sorry No Results';
}
?>
</div>
</body>
</html>
My issue is where you see '. $row['area'] .' displays nothing, but the '. $row['block_number'] .' works just fine.
Your query is only selecting block_number.
Try changing:
$sql = mysql_query("SELECT DISTINCT block_number FROM platform_locations WHERE area='$area'");
To:
$sql = mysql_query("SELECT DISTINCT block_number, area FROM platform_locations WHERE area='$area'");
Edit: If you have this issue in the future try var_dump($row); to see what the array contains. This would show you that you only have access to the block_number and not the area.
Double edit: I didn't notice, but the other answer is right about the $area var- you've already got the $area saved, use that variable instead of the return from the DB as it's already in memory. If this could change per record, it'd be prudent to use the record's area variable to make your code more reusable. However, in this particular case, your SQL statement has the area in the where clause, so it wont vary unless you attempt to use portions of this code elsewhere.
Your SQL query is only selecting block_number, so that's the only field that will be in the $row array. You've already got area as a variable $area so use that, not $row['area'].
I'm using this while statement to show all contents of database...
$sql = mysql_query("SELECT id,question,date,user,numberofcomments,body,locked FROM questions ORDER BY id DESC");
while($row=mysql_fetch_assoc($sql)) {
echo '<div class="comment">';
echo '<div class="leftpart">';
echo "<div class='date'><img src='../assets/icons/Time-info.png'> ".ago($row['date']);
echo "</div><br><img src='../assets/icons/User-Info.png'> ".$row['user'];
echo "<br><img src='../assets/icons/Comments.png'> ".$row[numberofcomments];
echo '</div>';
echo '<div class="rightpart-topic">';
if($row['locked']==1) { echo '<img src="../assets/icons/Lock.png" /> ';
}
echo ''.$row['question'].'';
echo '<br>'.substr($row['body'],0,70).'...';
echo '</div>';
echo '</div>';
}
I want to show only 10 rows then have links to show the rest (as in page 1,2,3,4, last type thing). How would I go about doing that? It would help if you could also explain your code as it would be greatly appreciated. It helps my learning process.
Thanks!
You need to do a LIMIT in your query using some sort of passed $_GET variable.
$start=0;
$limit = 10;
$page = (isset($_GET['page']) ? : 0));
$start = $page * $limit;
$sql = mysql_query("SELECT id,question,date,user,numberofcomments,body,locked FROM questions ORDER BY id DESC LIMIT {$start},{$limit}");
Should be pretty self explanatory.
If there is not page variable set then you are viewing page 0, or the first 10 results of your database query. If page = 1 then you are going to be viewing $page (value=1) * $limit (10) = $start (now value of 10), which will give you results 10-20 or page 1. etc.
So provide your user a button which can link them to a hard paginated link, or do it ajax.
Each url will need to be like so:
http://wwww.yoursite.com/somepage.php?page=1
Efficiency aside, you could simply set a counter and check if it value Is greater than 10 during the loop. If so, add a 'hidden' class to the parent div. That class would use display:none;
I have searched around and have found many solutions to my question, but I am still having one problem. I am trying to create a table that will have four images per row. This has been done. I have told the script to group by rewardid and it does that so now duplicate images do not appear (the steps to do these I have found on the site). What I am having problems with is that I m trying to put a multiplier under each image to count how many was in each group. (i.e. Lets say I have 5 medals. On my page, it only shows one medal, but I really have 5, so I would like for it to say x5 below the image.) This is what I have so far:
print "<div class='style1'><br> <br><h2>User Medals</div></h3><br />
<table width='80%' class='table' border='0' cellspacing='1'><tr bgcolor='gray'><th></th><th></th><th></th><th></th></tr>";
$result=mysql_query("SELECT * FROM awardsearned WHERE userid=$userid group by rewardid");
$count = 0;
while($res=mysql_fetch_array($result))
{
$award = $res['rewardpic'];
$award2 = $res['rewardid'];
$result2=mysql_query("SELECT awardid, count(rewardid) FROM awardsearned WHERE rewardid=$award2 AND userid=$userid");
$count2 = count($result2);
if($count==4) //three images per row
{
print "</tr>";
$count = 0;
}
if($count==0)
print "<tr>";
print "<td>";
print "<center><img src='$award' width='100' height='100'/><br />x$count2</center> ";
$count++;
print "</td>";
}
if($count>0)
print "</tr>";
print "
</table>";
Sorry if this is messed up, never posted here before. Here it is on pastebin if needed http://pastebin.com/iAyuAAzV
Update your query like this:
SELECT *,COUNT(rewardid) no_of_rewards
FROM awardsearned WHERE userid=$userid group by rewardid
$res['no_of_rewards'] will hold your number.
This will also eliminate the need for the second query.
These things are called "aggregate functions". More in the documentation.
To answer your question:
The value of $result2 would only give TRUE(1) or FALSE(0). If your query executed correctly $count2 will return 1, therefore you always get 1 in your code.
Try and change
$result2=mysql_query("SELECT awardid, count(rewardid) FROM awardsearned WHERE rewardid=$award2 AND userid=$userid");
$count2 = count($result2);
TO:
$result = mysql_query("SELECT awardid, count(rewardid) FROM awardsearned WHERE rewardid=$award2 AND userid=$userid");
$arr = mysql_fetch_array($result);
$count2 = count($arr);
This should give you the actual numbes of records from the resultset.
There are better ways of doing this (look at Bart Friederichs answer!) , like doing only SELECT and loop through resultset once. This would be for performance and for flexibility.
Also take in my consideration that mysql_query is deprecated now, and should not be used. (The preferal methods are to use PDO or mysqli instead)
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>';
}