I'm not really sure what the problem is here, I have a posts table that has a foreign key of userid and I want to pull their usernames from the users table based on that, it seems to pull them out fine but it won't display on the first post.
My code is:
$query_therealuserid = 'SELECT users.username, users.userid, posts.userid, posts.created_at
FROM users, posts
WHERE users.userid = posts.userid
ORDER BY posts.created_at';
$therealuserid = mysql_query($query_therealuserid, $akaearthdb) or die(mysql_error());
and
<?php do { ?>
<div id= "post">
<?php if (stripos($row_rsjustpost['link'], ".png") !== false) {?>
<img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
<?php }?>
<?php if (stripos($row_rsjustpost['link'], ".gif") !== false) {?>
<img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
<?php }?>
<?php if (stripos($row_rsjustpost['link'], ".jpg") !== false) {?>
<img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
<?php }?>
<?php if (stripos($row_rsjustpost['link'], ".jpeg") !== false) {?>
<img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
<?php }?>
<?php echo $row_rsjustpost['title']; ?>
<br/>
<?php echo $row_rsjustpost['text']; ?>
</p>
<br />
<?php
do {
if ($whileLoopCounter0!=$whileLoopCounter1){break;}
?>By: <?php echo $row['username'];
echo "<br />";
$whileLoopCounter1++;
} while($row = mysql_fetch_array($therealuserid));
?>
</div>
<?php $whileLoopCounter0++; ?>
<?php } while ($row_rsjustpost = mysql_fetch_assoc($rsjustpost)); ?>
</div>
when I pull up the page I get all the posts but the first one doesn't have a username and the usernames are all pushed down one post. The first post has a postid of 2, if I create a new post with a postid of 1 and a userid it shows up at the top without a username and the others usernames are moved up so that they are corect.
That's because you use a do-while loop, the $row = mysql_fetch_array($therealuserid) is executed at the end of the loop, why not use a regular while loop.
You're currently using a do-while loop, so first the do block is executed and then the while block is evaluated only after the 1st execution of the do block.
Use this instead:
while ($whileLoopCounter0==$whileLoopCounter1) {
$row = mysql_fetch_array($therealuserid);
if ($row){
?>
By:
<?php
echo $row['username'];
echo "<br />";
$whileLoopCounter1++;
}
}
Looks like you are fetching the username after you increment the loop counter. The user name is pulled after the loops is one, so the first row will be empty because that data doesn’t exist yet.
There are a lot of other things though that you’re doing that are just wasting effort though. Try tossing the results into an object and using foreach($result as $key=>$value) or foreach($result as $item) if you do not need the key instead.
Related
I want to get some data from a MySql database which has the same ID but different values. see the image (this is just a sample)
Although the venue styles are different, I want to pull all the styles with the same ID. I'm using a foreach loop to get the data from the database.
How can I improve my code to achieve what I want.
<?php
$myrows = $wpdb->get_results( "SELECT vf_venues.title, vf_venues.mainimage,
vf_venues.permalink, vf_venuestyles.slug
FROM vf_venues
LEFT JOIN vf_venuestyles ON vf_venuestyles.vid=vf_venues.vid WHERE
vf_venuestyles.vid=vf_venues.vid" );?>
<div class="venue-list venue-grid">
<?php
foreach ( $myrows as $myrow ) {
//pull the data from the DB
"<pre>"
$venueName = $myrow->title;
$mainImage = $myrow->mainimage;
$permalink = $myrow->permalink;
$slug = $myrow->slug;
$vid = $myrow->vid;
"<pre>"
?>
<li class="venue-block block">
<div class="venue-img">
<a href="<?php echo $permalink; ?>">
<img src="<?php echo $mainImage; ?>">
</a>
</div>
<div class="venue-details"><h2><?php echo $venueName; ?></h2></div>
<?php echo $slug; ?>
<?php echo $vid; ?>
</li>
<?php
}
?>
</div>
I managed to fix this by creating a for loop within the existing for loop. I then created a sql query to pull the venue styles for that venue.
I am working on a online shopping website.
This is a div that will display the item and the image should be also used from database.
Help me!
This is the code by which I am trying to fetch the data from mysql database created on phpmyadmin and construct this div.
The problem is that I can't see any output
<?php
include "connect_database.php";
$sql="SELECT * FROM product";
$result = mysqli_query($connection, $sql);
$rows=mysqli_fetch_assoc($result);
while ($rows=mysqli_fetch_assoc($result)) {
echo "<div class='item'>
<img class='image' src='".$rows["image"]."'><br/>
<span>".$rows["product_name"]."</span><hr/>
<span>".$rows["model_no"]."</span><hr/>
<span>Rs ".$rows["amount"]."</span>
</div>
";
}
?>
You call mysqli_fetch_assoc first before the while loop. That call reads the first row, but the result is not used. After that, the while loop kicks in and processes and outputs the rest of the rows. If you have only one row, you won't see anything.
So you can try this, with that line removed.
Note that I renamed $rows to $row. This isn't necessary, but since the variable will contain data of only one row, I think this name is better. It will help you understand the code better if you need to change it later.
<?php
include "connect_database.php";
$sql="SELECT * FROM product";
$result = mysqli_query($connection, $sql);
while ($row=mysqli_fetch_assoc($result)) {
echo "<div class='item'>
<img class='image' src='".$row["image"]."'><br/>
<span>".$row["product_name"]."</span><hr/>
<span>".$row["model_no"]."</span><hr/>
<span>Rs ".$row["amount"]."</span>
</div>
";
}
?>
echo '<div class="item">
<img class="image" src="'.$rows['image'].'"><br/>
<span>"'.$rows["product_name"].'"</span><hr/> <span>"'.$rows['model_no'].'"</span><hr/>
<span>Rs "'.$rows['amount'].'"</span>
</div>
';
<?php
include "connect_database.php";
$sql="SELECT * FROM product";
$result = mysqli_query($connection, $sql);
while ($rows=mysqli_fetch_array($result,MYSQLI_BOTH)) {
echo "<div class='item'>
<img class='image' src="<?php echo $rows['image']; ?>"><br/>
<span><?php echo $rows['product_name']; ?></span><hr/>
<span><?php echo $rows['model_no']; ?></span><hr/>
<span>Rs<?php echo $rows['amount']; ?></span>
</div>
";
}
?>
I am looking to offset a group of PHP array results to create multiple rows of data.
For example, the first row will contain the first four array results, with the second row displaying results 5-8 and the third row containing results 9-12.
Currently, I am printing out all 12 in one list, however I'd like a bit more display control (i.e. ordering the results into columns which I can style independent of each), hence why I'd like to offset the results.
My current PHP is below:
<?php
if (empty($tmdb_cast['cast'])) {
} else {?>
<section class="cast">
<p class="title">Cast</p>
<ul class="section_body"><?php
foreach($tmdb_cast['cast'] as $key => $castMember){
if ($key < 12) {?>
<li class="actor_instance clearfix"><?php
if ($castMember['profile_path'] != '') {?>
<img src="http://cf2.imgobject.com/t/p/w45<?php echo $castMember['profile_path']; ?>" class="actor_image pull-left" alt="<?php echo $castMember['name']; ?>" title="<?php echo $castMember['name']; ?>" /><?php
} else {?>
<img src="assets/images/castpic_unavailable.png" class="actor_image pull-left" alt="No image available" title="No image available" /><?php
}?>
<p class="actor"><?php echo $castMember['character']; ?> - <?php echo $castMember['name']; ?></p>
</li><?php
}
}?>
<div class="morecast pull-right">[...view all cast]</div>
</ul>
</section><?php
}?>
P.S. Apologies for how I've formatted the code in the above block - I'm still not 100% sure how to make it look "nice" on StackOverflow.
Use array_chunk to break a single dimension array into a 2D array. Then you can loop through each chunk, and then each result, to get that "offset" effect between them.
$chunks = array_chunk($tmdb_cast['cast'], 4); // 4 here, is the number you want each group to have
foreach($chunks as $key => $chunk)
{
foreach($chunk as $castMember)
{
//display castMember here
}
// add code between groups of 4 cast members here
}
First: mixing php and html this way is a very bad habbit... one day you will have to maintain your code and that day you will vomit all over your desk because you mixed different languages in one file...
That being said..
and without creating a new array:
foreach($tmdb_cast['cast'] as $key => $castMember)
{
if ($key < 12)
{
// your code goes here
// if key is 3, or 7 the code below will close the listcontainer and open a new one...
if( ($key+1)%4 == 0 AND $key <11)
echo '</ul> <ul class="section_body">';
}
}
Also replace this:
if (empty($tmdb_cast['cast']))
{
}
else {
with this:
if (!empty($tmdb_cast['cast']))
{
I'm not entirely sure what you're looking for but here's how I would format your code. Alternative syntax for 'if' is a little more readable, and the use of for loops instead of a foreach will give you the control over row numbers you say you're looking for.
<?php if( ! empty($tmdb_cast['cast'])): ?>
<section class="cast">
<p class="title">Cast</p>
<ul class="section_body">
<?php
for($i = 0; $i < 12; $i++):
$castMember = $tmdb_cast['cast'][$i];
?>
<li class="actor_instance clearfix">
<?php if($castMember['profile_path'] != ''): ?>
<img src="http://cf2.imgobject.com/t/p/w45<?php echo $castMember['profile_path']; ?>" class="actor_image pull-left" alt="<?php echo $castMember['name']; ?>" title="<?php echo $castMember['name']; ?>" />
<?php else: ?>
<img src="assets/images/castpic_unavailable.png" class="actor_image pull-left" alt="No image available" title="No image available" />
<?php endif; ?>
<p class="actor"><?php echo $castMember['character']; ?> - <?php echo $castMember['name']; ?></p>
</li>
<?php endfor; ?>
<div class="morecast pull-right">[...view all cast]</div>
</ul>
</section>
<?php endif; ?>
I want to retrieve data from database using ajax the data is retrieving successfully but it is not showing properly on page. Maybe it is the issue of if else condition.
Here is my code:
<?php
$query = mysql_query("SELECT * FROM cart_polling WHERE country = '".$_REQUEST['id']."' ORDER BY sort_order ASC") or die(mysql_error());
$record = mysql_num_rows($query);
while($getcontry = mysql_fetch_array($query)){
?>
<div class="Question_Table">
<div class="Question_Title">Q<?php echo $getcontry['sort_order']; ?>-<?php echo $getcontry['question']; ?></div>
<!-- ****** Box1 START ****** -->
<?php
if($getcontry['img1']!=""){
?>
<div class="bg">
<?php
if(isset($_SESSION['id'])){
?>
<div class="img"><center>
<a href="javascript:void(0)" class="op">
<img src="images/pollimg/<?php echo $getcontry['img1']; ?>" width="104" height="102" class="middleimg2" onclick="getValue('<?php echo $_REQUEST['id']; ?>','<?php echo $getcontry['option1']; ?>','<?php echo $getcontry['id']; ?>','1','<?php echo $getcontry['img1'] ?>','<?php echo $getcontry['categoryname'] ?>','<?php echo $getcontry['question']; ?>')" /></a>
</center></div>
<?php } ?>
<div class="Vote_button">
<img src="images/vote_button.png" onclick="sendvar();" align="middle">
<img src="images/view_results.png" onClick="showresult(<?php echo $getcontry['id']; ?>)" align="middle">
</div>
<?php
}
</div>
<?php } ?> // here the while loops end.
I want (<div id="txtHint<?php echo $getcontry['id']; ?>"></div>) this in else condition
and the above code in if condition when I click on image viewresult.png a function call which retrieve data from database. I want the data will show in else condition and the above code including loop shows in if condition.
The txtHint($getcontray['id']) is the id of every record whose value changes dynamically with the help of while loop. And txthint is also saves the response of ajax.
If I am using if condition than while loop is not running in else condition and the id can't pass to AJAX response and the correct data will not show, so how can I do that?
in line: <img src="images/pollimg/
'<?php echo $getcontry['img1'] ?>','<?php echo $getcontry['categoryname'] ?>'
missing 2 semicolon.
'<?php echo $getcontry['img1']; ?>','<?php echo $getcontry['categoryname']; ?>'
UPDATE:
And missing ?> php end syntax at the bottom of your file:
<?php
}
//here is the missing tag:
?>
</div>
<?php } ?> // here the while loops end.
So basicly what I'm trying to accomplish is that foreach row in mysql query it prints out the html with the data from that row. Here's what I have, it keeps giving me an error on my foreach.
<?php
$shots = mysql_query("SELECT * FROM shots") or die(mysql_error());
while($row=mysql_fetch_array($shots))
$data[]=$row;
foreach($shots as $data)
if (!empty($data)){
$id = $data["id"];
$shotby = $data["shot"];
$passby = $data["pass"];
$time = $data["time"];
?>
<div class="feedbody">
<div class="title"><?php echo $shotby; ?></div>
<div class="feed-data">: gets a pass from <span><?php echo $passby; ?</span> and he takes a shot!</div>
<img class="dot" src="images/dot.png" />
</div>
<?php
}
}
?>
Or something like that. Can anybody help point me in the right direction. I've been trying to find the answer.
EDIT: adding the error as requested.
Warning: Invalid argument supplied for foreach() in /home/content/93/7527593/html/fusionboard/includes/feed.php on line 7
First, if you want to access the data by name (instead of index), you need to include MYSQL_ASSOC as a second parameter to mysql_fetch_array, or use mysql_fetch_assoc.
Not really sure why you were copying the MySQL results to a second array just to loop through that later - you can loop through the results directly:
<?php
$shots = mysql_query("SELECT * FROM shots") or die(mysql_error());
while($row = mysql_fetch_assoc($shots)) { ?>
<div class="feedbody">
<div class="title"><?php echo $row["shot"]; ?></div>
<div class="feed-data">: gets a pass from <span><?php echo $row["pass"]; ?></span> and he takes a shot!</div>
<img class="dot" src="images/dot.png" />
</div>
<?php } ?>
Update after you posted the error message: the error from your original code was that you first went through and copied each result row into $data, but then in your foreach you tried to loop on $shots (again) and have it call each item $data.
What you probably wanted to do was have foreach ($data as $item) and then copy the properties from $item.
Something like this?
<?php
$shots = mysql_query("SELECT * FROM shots") or die(mysql_error());
while($row=mysql_fetch_assoc($shots))
{
$id = $row["id"];
$shotby = $row["shot"];
$passby = $row["pass"];
$time = $row["time"];
?>
<div class="feedbody">
<div class="title"><?php echo $shotby; ?></div>
<div class="feed-data">: gets a pass from <span><?php echo $passby; ?</span> and he takes a shot!</div>
<img class="dot" src="images/dot.png" />
</div>
<?php
}
?>
Perhaps you need another closing brace? (Another "}" at the end, I mean).
You saved your data in the $data variable, but your foreach uses the $shots variable.
Just change it to foreach($data as $something) and $something["id"] (for example) to retrieve a value
you are using one variable instead of another.
and many useless code.
while youneed only
<?php foreach($data as $row) { ?>
<div class="feedbody">
<div class="title"><?php echo $row['shotby'] ?></div>
<div class="feed-data">:
gets a pass from <span><?php echo $row['passby'] ?</span> and he takes a shot!
</div>
<img class="dot" src="images/dot.png" />
</div>
<?php } ?>