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.
Related
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?
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 have been trying to solve this problem and have find a lot of solutions but still can't get to work. I am so frustrated with the codes and I think I will just do it step by step then instead of lumping it altogether and get it work at once.
Right now my tables are:
categories | name
fashion | chanel
fashion | prada
cafes | starbucks
cafes | tcc
dining | kfc
dining | macdonalds
What I want to achieve is
1) Echo-ing out the categories <h1>fashion</h1> with once instead of duplicating and <ul> if it is starting on a new category
2) Echo-ing out all the <li>name</li> no matter how many of names are under that fashion
3) Echo-ing out </ul> if it is the last name in that fashion
And so on for the rest of the categories. This is currently what my mind is thinking of and I do not know whether is this practical or not.
I have tried using this way:
$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='$level' ORDER BY categories");
while($row = mysqli_fetch_array($result)){
if($previousVal != $row['categories']){
$data .= '<h1>'.$row['categories'].'</h1><ul class="shop_listing">';
$previousVal = $row['categories'];
}
$data .= '<li>
<p class="float_left">'.$row['name'].'</p>
</li></ul>';
}
Which I know there is something wrong in the looping of the li with the closing ul. This is where I am stuck for very long time and unable to find any solution anywhere. No matter what I do, foreach for different categories, if else, I still can't get anywhere near.
The result I get is either:
<h1>fashion</h1>
<ul class="shop_listing">
<li><p>chanel</p></li>
</ul>
<li><p>prada</p></li>
<h1>dining</h1>
<ul class="shop_listing">
<li><p>kfc</p></li>
</ul>
<li><p>macdonald</p></li>
Or:
<h1>fashion</h1>
<ul class="shop_listing">
<li><p>chanel</p></li>
<li><p>prada</p></li>
<h1>dining</h1>
<ul class="shop_listing">
<li><p>kfc</p></li>
<li><p>macdonald</p></li>
</ul>
</ul>
Hope you guys can help me out please. For those who just put links and commented read more on that, don't input your answers here. I don't need that, I am already very confused and I am just a beginner in this. I am glad if someone is willing to guide or even explain to me why my code doesn't work and why the solutions given will work. Thanks in advance!
One way to do it is create a new array using your category as a key and put all names belonging to that category into an array within that. Loop through the new array and create your HTML.
PHP
$temp_array=array();//temporary array
$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='$level' ORDER BY categories");
while($row = mysqli_fetch_array($result)){
$temp_array[$row['categories']][]=$row['name'];//put data into temporary array
}
foreach($temp_array AS $category=>$names){
$data.='<h1>'.$category.'</h1><ul class="shop_listing">';
foreach($names AS $name){
$data.='<li>
<p class="float_left">'.$name.'</p>
</li>';
}
$data.='<ul>';
}
Not tested, but it should do what you need.
Try this
$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='$level' ORDER BY
categories");
while($row = mysqli_fetch_array($result)){
if($previousVal != $row['categories']){
if($data) {
$data .= "</ul>";
}
$data .= '<h1>'.$row['categories'].'</h1><ul class="shop_listing">';
$previousVal = $row['categories'];
}
$data .= '<li>
<p class="float_left">'.$row['name'].'</p>
</li>';
}
$data .= "</ul>";
I'm an interaction designer building out front_end HTML design. I am new to PHP, but old hand with HTML and quick to new technology. I'm hoping someone can help me out. I've done a few hours of research and can't find the answer to in any forum so far to my very specific problem.
GOAL: I'd like a nice way to display various discrete cells of data from my database into my HTML designs. Specifically, I'd like to be able to display a given 'title' on my page design by referencing a unique id in the same row of the database. Here's what one row looks like...
'unique_id'
'title'
'description'
'image'
What I need is a simple way to pull individual cells from an array using some small code snippet. Something like this:
<li class="active"><?php echo [unique_id][title];?></li>
...where unique_id would work as an index that would tell the array which row to look at, and then display the title from that same row.
I have a hunch it has to do with indexed arrays and some special loops, but I am very lost. Your help, but more importantly teaching me so I can learn, would be most appreciated.
Here's what I have currently, but it doesn't work because it's only going off the incremental row order (0..., 1..., 2...). What I need is for those numbers to be unique_ids for the rows, like (1005..., 104..., 106...).
<?php
$sql = "SELECT title FROM phones";
$result = mysql_query($sql) or die(mysql_error());
$array = array();
while($row = mysql_fetch_assoc($result)) {
$array[] = $row;
}
?>
<ul class="nav nav-list">
<li class="nav-header">iOS Phones</li>
<li class="active"><?php echo $array[0][title];?></li>
<li><?php echo $array[1][title]?></li>
Christopher
As far as I understand, you wish to list the titles along with their unique ID's?
It can be achieved using MySQL & PHP like this:
<?php
$sql = "SELECT * FROM phones";
$result = mysqli_query($sql);
echo "<ul>";
while ($row = mysqli_fetch_array($result)) {
echo "<li><a href='#'>" . $row["unique_id"] . " - " . $row["title"] . "</a></li>";
}
echo "</ul>";
?>
This will output something like this:
<ul>
<li><a href='#'>100 - HelloWorld</a></li>
<li><a href='#'>101 - AnotherTitle</a></li>
</ul>
And so forth.. You can order the outputs alphabetically by title or by the unique ID's. This can be done by adding ORDER BY title or ORDER BY unique_id to the end of your $sql variable.
What it does is just loop through all the database entries received from SELECT * FROM phones. Every row will be put into a $row array.
I hope this helped a bit.
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.