I have a blog with 3 posts per page, and I'd like to show a single advert above each post.
The posts are selected from a MySQL table using PHP, and I can get the posts to echo out propely, but is there a way of showing different adverts within the loop?
Here is my code, and an example of what I'm trying to achieve:
$ad_1 = "Advert 1";
$ad_2 = "Advert 2";
$ad_3 = "Advert 3";
$posts .= "
<div class=\"postDivide\"></div>
<div class=\"advert\"> $ad_1 then $ad_2 then $ad_3 </div> // Here Is The Problem
<div class=\"postDivide\"></div>
<div class=\"post\">
// Post Content
</div>
";
echo $posts;
Is there a correct way of showing each advert?
Related
I am trying to create a small website to sell items on and learn php. I have a index.php which links to a chairs.php page and then from there to a viewItem.php page depending on which item they choose.
I have in my database 4 items. On the chairs.php a while loop runs to display on the page all items in that database.
I would like to use the id of the item to link to the next page but I think the while loop is always going to assign to the variable that I am using the very last entry in that while loop.
Can someone point me in the direction of how I would resolve this? I would like the user to select a Chair 1 on my Chairs.php page with ID 1 in my db to link to the corresponding page. Currently all links on the chairs.php would link to the last item in my while loop which is then assigned to the $_SESSION['output_id'] = $output_id variable.
Thank You
<?php
$query = "SELECT * FROM `chairs_table";
$queryChairs = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($queryChairs)) {
$output_id = $row["ID"];
$output_img = $row["Image"];
$output_name = $row["Name"];
$output_desc = $row["Desc"];
$output_price = $row["Price"];
$_SESSION['output_id'] = $output_id;
$_SESSION['output_img'] = $output_img;
$_SESSION['output_name'] = $output_name;
$_SESSION['output_desc'] = $output_desc;
$_SESSION['output_price'] = $output_price;
?>
<div class="itemContainer" onclick="location.href = 'viewItem.php?id=<?php echo "{$output_id}" ?>'" style="cursor:pointer;" >
<div> <img src = "img/<?php echo "{output_img}" ?>"> </div>
<div class="itemTextTitle"><?php echo"{$output_name}" ?></div>
<div class="itemTextDesc"><?php echo"{$output_desc}" ?></div>
<div class="itemTextPrice">cop <?php echo"{$output_price}"?></div>
</div>
<?php
}
?>
</div>
Been trying to figure out what's wrong for nearly two hours now.
I have made a wall post feature for my website, posts should be displayed on the profile wall, however instead of all of the posts being displayed, only 1 is. I have no idea what is wrong with my code, I can't find any issues.
The $profile_username variable has been defined in previous code and works. Even if I just type the name of the profile in it still only returns one post.
//----------submit wall post----------//
$getwallposts = mysql_query("SELECT * FROM `wallposts` WHERE `postedto`='$profile_username' ORDER BY `postid`");
//check if any rows are returned
while($wallposts = mysql_fetch_assoc($getwallposts)) {
$postid = $wallposts['postid'];
$postedby_username = $wallposts['postedby'];
$wallpostdate = $wallposts['dateposted'];
$wallpost = $wallposts['post'];
$querypostedby_info = mysql_query("SELECT * FROM `users` WHERE `username`='$postedby_username'");
//get the info above
if (mysql_num_rows($querypostedby_info) > 0) {
$getpostedby_info = mysql_fetch_assoc($querypostedby_info);
$postedby_id = $getpostedby_info['id'];
$postedby_profilepicture = $getpostedby_info['profilepicture'];
}
//lets keep the line breaks
$wallpost=nl2br($wallpost);
//display the posts
$wallpoststicker =
"
<div id='wallpost-container'>
<div id='wallpost-header'>
<img src='$postedby_profilepicture'><div id='wallpost-header-by'><a href='/profile.php?id=$postedby_id'>$postedby_username</a> said:</div>
<div id='wallpost-date'>• $wallpostdate</div>
</div>
<div id='wallpost-content'>
$wallpost
</div>
</div>
";
}
Also, I know that mysql is deprecated; my version of PHP can handle mysql queries fine though.
Where to do you echo you're output?
If it is after the While you will only get the last result that is stored in the variable.
Output within the while loop.
I can see a couple of issues with that code. First, what it seems to be your problem:
$wallpoststicker =
"
<div id='wallpost-container'>
<div id='wallpost-header'>
<img src='$postedby_profilepicture'><div id='wallpost-header-by'><a href='/profile.php?id=$postedby_id'>$postedby_username</a> said:</div>
<div id='wallpost-date'>• $wallpostdate</div>
</div>
<div id='wallpost-content'>
$wallpost
</div>
</div>
";
This code overrides the $wallpoststicker variable on every loop. That's why you only print one result at the very end.
But secondly, you're doing a query within a loop, which is potentially very inefficient. Have you thought on doing a LEFT JOIN between "wallposts" and "users" so you have all the data you need before you start your loop?
This is probably an easy question but does anyone know how to insert data from a database into tabs autonomously ordered by a certain field?
Let me clarify, I need each tab to display data from my database ordered by the date they were added. So the latest in tab 1, the next in tab 2, and so on.
I've had this working in an accordion style where I did the database query first with a limit of five which repeated my code that inserted the info from the database up to five times. I have been trying to figure out a way similar to this except with tabs instead. Even if I could get a database query to put the latest result in tab 1, then do another query in tab 2 to find the second newest result, and so on.
Thanks for your time. Sorry if this is a silly question. :)
Sorry for leaving out code. What I am hoping to do is to call data from a database with code simular to
<?php
$subject_set = mysql_query("Select * FROM database WHERE column1 like 'value' ORDER BY date_added DESC LIMIT 4", $connection);
if (!$subject_set){
die("Database connection failed: " . mysql_error());
}
while ($subject = mysql_fetch_array($subject_set)){?>
<ul class="tab-links">
<li class="active">Tab #1</li>
<li>Tab #2</li>
<li>Tab #3</li>
<li>Tab #4</li>
</ul>
<div class="tab-content">
<div id="tab-1" class="tab active">
<?=$subject['column1']?>
<?=$subject['column2']?>
<?=$subject['column3']?>
<?=$subject['column4']?>
</div>
<div id="tab-2" class="tab">
<?=$subject['column1']?>
<?=$subject['column2']?>
<?=$subject['column3']?>
<?=$subject['column4']?>
</div>
<div id="tab-3" class="tab">
<?=$subject['column1']?>
<?=$subject['column2']?>
<?=$subject['column3']?>
<?=$subject['column4']?>
</div>
<div id="tab-4" class="tab">
<?=$subject['column1']?>
<?=$subject['column2']?>
<?=$subject['column3']?>
<?=$subject['column4']?>
</div>
</div>
I was hoping for code similar to this or probably only have one tab which is repeated depending on the limit in the database query.
First, the query:
SELECT column1, column2 FROM tableName ORDER BY dateColumn DESC LIMIT 5;
Let's assume you put this data into an array called $data.
If your tabs are HTML <li> elements then you can create a function for creating the HTML:
function buildList($data){
$output = "";
if(!empty($data)){
$output .= "<ul>";
foreach($data as $row){
$output .= "<li>" . $row . "</li>";
}
$output .= "</ul>";
}
return $output;
}
I don't think I can provide any more detail without knowing the structure of your table, or the expected output.
I'm new to PHP and pardon me for asking this very basic question. What I want to do is to display or view a page based on a specific record. For example, I have a home.php page which lists records of lessons. And when I click on a specific record, it will go a page named lesson.php . I have to view the relevant information/data from my dB of that specific lesson. I tried to use GET but I think it's not going to meet the requirement of my system.
This is what I've tried so far:
$qry1stQuarter = $conn->prepare("SELECT l.lesson_title FROM tbllessons as l
JOIN tblstudents as s
ON l.grade_level = s.grade_level
WHERE quarter_code = '1st'
AND s.grade_level=:grade_level");
$qry1stQuarter->execute(array(':grade_level' => $grade_level));
<div id="tabs-2">
<div id="accordion">
<h3><strong>Yunit 1</strong></h3>
<div>
<?php
for($i=0; $row = $qry1stQuarter->fetch(); $i++){
$lesson_title = $row['lesson_title'];
?>
<div id = "lessons">
<?php
echo "<a href = 'lesson_view.php'>$lesson_title </a>";?>
</div>
<?php
} // end of for loop
?>
</div> <!-- end of Yunit 1 -->
What is the best way to do this? Your help is pretty much appreciated. Thanks.
In your database, I assume you have an ID column. A typical way to do what you are asking is to use that ID as a GET parameter on a link, and then include that in your WHERE clause in your SQL statement.
Eg:
echo "<a href='lesson_view.php?id=$lesson_id'>$lesson_title</a>";?>
And then on your lesson_view.php page, your SQL has something like this:
SELECT * FROM tbllessons WHERE id = mysql_real_escape_string($_GET['id'])
I am trying to figure out a way to split mySQL rows into HTML columns. I'm sorry if there is an easier way to ask this, but I can't wrap my head around it.
I have read through very many suggestions though none of them fit what I am looking for.
I understand it is very easy to just float the information left and allow it to stack by itself. My problem is that I am showing information in a "card" form with two different sized cards and I would like them all to stack without gaps vertically.
For the purpose of showing this, I've given every card a number from 1-3. Every card numbered 1 should fit in the first column, and so on. example here
Normal floating left gives me something like this:
|1|2|3|
|4|5|6|
|7|8|9|
This is the correct way to display the information, but it leaves gaps where large cards are next to small ones like in the example.
if I use a foreach loop in the php with a table or divs, I get something like this:
|1|2|3|
-------
|4|5|6|
-------
|7|8|9|
Unfortunately, this is pretty much the same outcome. The larger cards push down the row leaving a large gap where a smaller card is.
I also understand that after the first visibile row, the most recent divs could potentially be pushed farther down in one column rather than another. for example, if column 1 has 6 'featured' or large cards, it would take 12 cards to fill the space beside it. I'm not worried about this.
The basic, stripped down code that I have right now is this:
<div class="container">
<?PHP
$qry_questions = mysql_query("SELECT STATEMENT**");
$count = 0;
while($row = mysql_fetch_array($qry_questions)){
$count++;
/*GATHER INFORMATION*/
?>
<div class="HTML-formatting-here-with-php-echos">
</div>
<?PHP
}
?>
</div>
Now I'm just trying to figure out if I can make a statement in php that basically says:
"if $count == 1, append this information into column 1, if $count == 2, append this into col 2, etc."
Is there any type of function that I can do to split this information like this and append the information without completely building a whole new row?
p.s. I've tried array_chunk() without the proper outcome.
EDIT: Here is the while loop
<div id="collective">
<div class="container">
<?PHP
//FIND THE QUESTIONS ASKED AND INFORMATION ON THEM
$qry_questions = mysql_query("SELECT * FROM mbr_qa_questions ORDER BY q_votes DESC" /* LIMIT 0, 10"*/); //LIMIT 0, 20 - 20 being how many to show at a time
$num_rows = mysql_num_rows($qry_questions);
$max_col = 3;
$count = 0;
//START THE LOOPING
while($q_row = mysql_fetch_array($qry_questions)){
$q_id = $q_row['q_id'];
$q_votes = $q_row['q_votes'];
$q_answers = $q_row['q_answers'];
$q_title = $q_row['q_title'];
$q_description = $q_row['q_description'];
$q_tags = $q_row['q_tags'];
$q_user_id = $q_row['user_id'];
$q_created = $q_row['q_created'];
$q_modified = $q_row['q_modified'];
$q_featured = $q_row['q_featured'];
if($q_featured == 0){
$q_status = "normal";
} else {
$q_status = "featured";
}
//GET THE USERS USERNAME
$qry_username = mysql_query("SELECT user_name FROM mbr_user_name WHERE user_id = '$q_user_id'");
$q_user_name = mysql_result($qry_username, 0);
//FIND THE USERS POINTS
$qry_points = mysql_query("SELECT point_amount FROM mbr_qa_points WHERE user_id = '$q_user_id'");
$q_user_points = mysql_result($qry_points, 0);
//FIND OUT IF YOU FLAGGED IT
$qry_flagged = mysql_query("SELECT * FROM mbr_qa_flagged WHERE q_id = '$q_id' AND user_id = '$user_id'");
$flagged = mysql_num_rows($qry_flagged);
if($flagged >= 1){
$flaggedDiv = "<div class='q_flagged'> </div>";
} else {
$flaggedDiv = "<div class='flagInnapropriate'> </div>";
}
//FIND HOW MANY ANSWERS
$qry_answers = mysql_query("SELECT * FROM mbr_answers WHERE q_id = '$q_id'");
$q_answers = mysql_num_rows($qry_answers);
//FIND HOW MANY VOTES
$qry_votes = mysql_query("SELECT * FROM mbr_votes WHERE q_id = '$q_id'");
$q_votes = mysql_num_rows($qry_votes);
//GIVE EACH ENTRY A COLUMN NUMBER
$count++;
if($count >= 4){
$count = 1;
}
<div class="card <?= $q_status ?>">
<div class="top">
<div class="tag">
<div class="title">Votes</div>
<div class="votes">
<div class="number">
<?= $q_votes ?>
</div>
</div>
<div class="plus">+</div>
</div>
<div class="question">
<p><?= $count/* . $q_title*/ ?></p>
</div>
</div>
<div class="middle">
<div class="elapsed">
<?PHP
$time_since = $q_created;
$time_now = time();
$timeElapsed = $time_now - $time_since;
?>
Asked <?= elapsedTime($time_since) ?> ago by
</div>
<a class="profile" href="./profile.php?profile_name=<?= $q_user_name ?>">
<div class="avatar">
<img src="pieces/avatars/avatar.php?profile_id=<?= $q_user_id ?>&size=xsmall" />
</div>
<div class="userName">
<?= $q_user_name ?>
</div>
<div class="points">
<?= $q_user_points ?>pts
</div>
</a>
</div>
<div class="bottom">
<div class="answer">
<div class="title">
Answers
</div>
<div class="numAnswers">
<?= $q_answers ?>
</div>
<div class="answersText">
Answers
</div>
</div>
<div class="bestAnswer">
<div class="title">
Best Answer
</div>
<div class="description">
<p>Need to get highest rated answer here</p>
</div>
</div>
</div>
</div>
}
?>
</div>
</div>
The included newAnswerLayout.php is just the html formatting of each card.
ANOTHER EDIT:
I believe I just need to make three mySQL queries that select every nth row. Yes, the IDs on the rows are unique, but there is a possibility that some rows will be moved to another table which will leave gaps.
What I'm thinking now is to query the table, pull every third row, place that in column 1 and so forth, starting at the first entry.
EDIT 3:
What I am looking for is to do a SQL query that grabs all of the information on every third row, starting on row 1, then I need to do another query of every third row, starting on row 2, then the same for row 3.
use float:left; in your file
http://beta.mybattlereport.com/styles/global.css
line 82
class container
EDIT :
first: those cards with numbers 1,2,3 they all have same class card normal so if you apply changes to one card it will aplly changes to all cards
solution : try using table instead of divs.
second : your cards are ordered like that
card1 card2 card3 featured card , card1 card2 card3 featured card
solution : this can also be fixed by table and you order them
1 column will be for card1
2 column will be for card2
3column will be for card3
and then you can use other table under this for featured cards or a div.
EDIT2>
your problem is in the sql to get those cards generated by number of cards and featured
you should order them first by featured and then by votes
try this
$qry_questions = mysql_query("SELECT * FROM mbr_qa_questions ORDER BY q_featured ,q_votes DESC" /* LIMIT 0, 10"*/);
the numbers 1 and 2 and 3 you are just giving numbers to each card . so i dont see any purpose from those numbers why you are making them.