I am making a system and I need to see information for each student. When I click the button, I need to know the student's code and open a page (seeStudent.php) with the student's information. Can someone help me?
<?php
$sql = $conn->query("SELECT * from tb_coordinator inner join tb_teacher
on tb_coordinator.cd_coord = tb_teacher.cd_coord
inner join teacher_class on
tb_teacher.cd_teacher = teacher_class.cd_teacher
inner join tb_teacher on
teacher_class.cd_class = tb_class.cd_class
inner join tb_student on
tb_class.cd_class = tb_student.cd_class
where tb_teacher.cd_cpf = '$cpf' and nm_class = '3a1' order by cd_number asc");
while($row = mysqli_fetch_array($sql)){
$nm_student = $row['nm_student'];
$cd_number = $row['cd_number'];
?>
<ul class="list-group list-group-flush" style="width: 50%; margin-top: 2%;">
<div id="load_student">
<li class="list-group-item"><?php echo $cd_number . " - " . $nm_student;?>
Get Information</button></li>
</div>
</ul>
</div>
<?php }?>
You need a bit modification in your prepared HTML. Add your required parameters to the anchor href which might help you to get student details from database in seeStudent.php page.
<ul class="list-group list-group-flush" style="width: 50%; margin-top: 2%;">
<div id="load_student">
<li class="list-group-item"><?php echo $cd_number . " - " . $nm_student;?>
<a href="seeStudent.php?cd_number=<?=$cd_number?>&nm_student=<?=$nm_student?>" >
<button type="button" class="btn btn-outline-info" style="margin-left: 90%;">Get Information</button>
</a>
</li>
</div>
</ul>
If you click Get Information button, it will be taken you to seeStudent.php with two $_GET parameters (cd_number, nm_student).
Now the final task. In seeStudent.php page take the parameter from $_GET array and do other query to get student details from database and to display.
seeStudent.php
<?php
$cd_number = isset($_GET['cd_number']) ? $_GET['cd_number'] : '';
$nm_student= isset($_GET['nm_student']) ? $_GET['nm_student'] : '';
// .... code stuff, possibly query and display
?>
Related
Im trying to Hide a comments that are not releated with the post above, the problem is that I have a database with all comments, and they are present in every post that I add... I'm trying to add a data-ID for every comments and trying to create a PHP "if" inside a generator post that if doesn't match doesn't show, but I think that i'm going to complicate myself, please help me:
this is a PHP function that create a comment and as you can see I added a data-POSTER that rappresent the ID of the post:
function createCommentRow($data,$utenteloggato) {
global $conn;
if ($utenteloggato == $data['userID']) {
$response = '
<div class="comment" data-postER="'.$data['postID'].'" >
<div class="user">'.$data['name'].' <span class="time">'.$data['createdOn'].'</span></div>
<div class="userComment" >'.$data['comment'].'</div>
<div class="reply">REPLY</div>
<div class="replies">
<a id="option1"
data-id="'.$data['id'].'"
data-option="'.$data['tipo'].'"
href="javascript:void(0)"
onclick="goDoSomething(this);">
Delete
</a> ' ;
}
else
{
$response = '
<div class="comment" data-postER"'.$data['postID'].'" >
<div class="user">'.$data['name'].' <span class="time">'.$data['createdOn'].'</span></div>
<div class="userComment" data-postID="'.$data['postID'].'">'.$data['comment'].'</div>
<div class="reply">REPLY</div>
<div class="replies">
' ;
}
$sql = $conn->query("SELECT replies.id, name, comment, tipo, DATE_FORMAT(replies.createdOn, '%e/%c/%Y %T') AS createdOn, userID, postID FROM replies INNER JOIN users ON replies.userID = users.id WHERE replies.commentID = '".$data['id']."' ORDER BY replies.id DESC LIMIT 1");
while($data = $sql->fetch_assoc())
$response .= createCommentRow($data,$utenteloggato);
$response .= '
</div>
</div>
';
return $response;
}
inside ad another php I show all the post from database table "post", everything work fine, but inside this div where the comments go to display:
<div class="userComments" data-postID="'.$data['id'].'" > </div>
I want put a IF condition that if the value of data-postER from the comments doesn't much with the data-postID from class="userComments" it doesn't show up. Thanks to everybody
Since you are creating a big piece of html inside the $response...
How about, adding (just for the example I will use line by line) few or one line at a time with an if when coming in the line of userComment so for example:
else
{
$response = '
<div class="comment" data-postER"'.$data['postID'].'" >
<div class="user">'.$data['name'].' <span class="time">'.$data['createdOn'].'</span></div>
<div class="userComment" data-postID="'.$data['postID'].'">'.$data['comment'].'</div>
<div class="reply">REPLY</div>
<div class="replies">
' ;
}
this becomes this:
else {
$response .= '<div class="comment" data-postER"'.$data['postID'].'" >';
$response .= '<div class="user">'.$data['name'].' <span class="time">'.$data['createdOn'].'</span></div>';
if($data['postID'] == $data['id']){
$response .= '<div class="userComment" data-postID="'.$data['postID'].'">'.$data['comment'].'</div>';
}
$response .= '<div class="reply">REPLY</div>';
$response .= '<div class="replies">';
}
This and now you either created the userComments or didn't...
You can ofcourse insert more than one line at a time but I wanted to illustrate it...
Also I would recommend not building the whole html inside a variable... rather building the html and inserting variables into it like this:
?><div class="comment" data-postER="<?php echo $data['postID'];?>" >
This way you embed php variables inside a healthy lookin html ...:)
Solved!!
Thanks, guys. This was my first question on here. Let me know if I'm not formatting this correctly or something. Here's my finished code:
Answer:
$sql = 'SELECT a.*, GROUP_CONCAT(t.name) as track_name FROM albums AS a LEFT JOIN tracks AS t ON a.album_id = t.album_id GROUP BY a.album_id';
foreach ($conn->query($sql) as $row) { ?>
<div class="container">
<div class="row">
<div class="col-6">
<img src="../images/<?= $row['album_cover']?>" alt="Card image cap" style="width:100%;">
<!--shadow-->
<div class="shadow-lg p-3 mb-5 bg-white rounded">
<div class="card-body" style="padding-left:10px;">
<h4 class="card-title"><?= $row['album_name'] ?></h4>
<b><?= print_r($row['album_name'] . ' (' . $row['record_label'] . ') (' . $row['year_released'] . ')', true); ?><br><br></b>
<ul class="list-group list-group-flush">
<?php
$tracks = explode(",", $row['track_name']);
$numTracks = count($tracks);
$i = 0;
while ($i < $numTracks) { ?>
<li class="list-group-item">
<?php
echo $tracks[$i];
$i++;
?>
</li>
<?php } ?>
</ul>
</div>
</div>
</div>
</div>
</div>
<?php } ?>
Question (Solved): I am working on a project for school. I have to revamp an existing discography site (Queen) using php. The part I'm stuck on now is pulling album info from a database and using it to populate the page with each album along with the track names for the album. I have my album info in one table called albums, and all the tracks for every album in another table called tracks, with album_id as a foreign key.
I can get to the point where it's generating the album title from the album table in my database, but I have no idea how to get the tracks for each album.
This is my SQL query:
$sql = '
SELECT a.album_id
, a.album_name
, a.year_released
, a.record_label
, a.album_cover
, t.name
, t.album_id
FROM albums AS a
LEFT
JOIN tracks AS t
USING (album_id)
';
And here is the part in that is supposed to generate the albums:
<?php
foreach ($conn->query($sql) as $row) { ?>
<div class="col-6">
<img src="../images/<?= $row['album_cover']?>" alt="Card image cap" style="width:100%;">
<!--shadow-->
<div class="shadow-lg p-3 mb-5 bg-white rounded">
<div class="card-body" style="padding-left:10px;">
<h4 class="card-title"><?= $row['album_name'] ?></h4>
<b><?= print_r($row['album_name'] . ' (' . $row['record_label'] . ') (' . $row['year_released'] . ')', true); ?><br><br></b>
<ul class="list-group list-group-flush">
<li class="list-group-item">Innuendo</li> <!-- these are just placeholders for the tracks because I don't know what to do here! -->
<li class="list-group-item">I'm Going Slightly Mad </li>
<li class="list-group-item">Headlong</li>
<li class="list-group-item">I Can't Live With You</li>
<li class="list-group-item">Don't Try So Hard </li>
<li class="list-group-item">Ride The Wild Wind</li>
<li class="list-group-item">All God's People</li>
<li class="list-group-item">These Are The Days Of Our Lives</li>
<li class="list-group-item">Delilah</li>
<li class="list-group-item">The Hitman</li>
<li class="list-group-item">Bijou</li>
<li class="list-group-item">The Show Must Go On </li>
</ul>
</div>
</div>
</div>
<?php
}
?>
As it stands, my page is being populated by the same album info over and over because the foreach is iterating through every row in both tables. How do I generate specific tracks based on unique albums?
Try using ON instead of USING:
$sql = 'SELECT * FROM albums AS a LEFT JOIN tracks AS t ON a.album_id = t.album_id';
This should join the two tables correctly. If there are multiple tracks for each album you will need to either query them separately or join the tracks into a group using GROUP_CONCAT. Here would be the query to accomplish this:
$sql = 'SELECT a.*, GROUP_CONCAT(t.name) as track_name FROM albums AS a LEFT JOIN tracks AS t ON a.album_id = t.album_id GROUP BY a.album_id';
Just as a side note I HIGHLY recommend using PDO for all sql related things. Here is a link to set it up and use it for safer sql queries: https://www.w3resource.com/php/pdo/php-pdo.php
ADDED TO LOOP THROUGH TRACKS
First you would need to take the variable and explode it into an array (place inside your loop):
$tracks = explode(',',$row['track_name']);
foreach($tracks as $track_name){
echo $track_name;
}
Now you should be able to do anything you want with these tracks. You could easily place them in a table or in a div.
You call $conn->query($sql) every loop which will return the whole result set again. Also, use while loop like below, not foreach loop since $conn->query() return a mysqli_reuslt object, not an array.
$sql = 'SELECT a.album_id, a.album_name, a.year_released, a.record_label, a.album_cover, t.name, t.album_id
FROM albums AS a LEFT JOIN tracks AS t USING (album_id)';
$result = $conn->query($sql) // return a mysqli_result object
while($row = $result->fetch_assoc()) {
// Your loop content
}
If you insist on using foreach loop:
$result = $conn->query($sql) // return a mysqli_result object
$rows=$result->fetch_all(MYSQLI_ASSOC); //return an array
foreach($rows as $row) {
//content
}
Two ways in which you could approach this problem:
Have 2 loops - the first looping through a simple query to the albums table (no joins), then within that loop perform a second query to the database to get all the tracks. (The downside being that you will be making more queries to the database)
Alternatively, change your query so you are selecting from the tracks table and then the join is on the albums table. To have it so the album title is only printed once for each album, you would need to sort the tracks by album, and then have an if statement to check when the loop has started processing a different album.
You must change your design a little.
First you get the first row get the album data and the first track
After that you grap the rest of the track and close the html tags
$sql = 'SELECT a.album_id, a.album_name, a.year_released, a.record_label
,a.album_cover, t.name, t.album_id
FROM albums AS a LEFT JOIN tracks AS t USING (album_id)';
$result = $conn->query($sql) // get a result from the mysql server
$row = $result->fetch_assoc();
?>
<div class="col-6">
<img src="../images/<?= $row['album_cover']?>" alt="Card image cap" style="width:100%;">
<!--shadow-->
<div class="shadow-lg p-3 mb-5 bg-white rounded">
<div class="card-body" style="padding-left:10px;">
<h4 class="card-title"><?= $row['album_name'] ?></h4>
<b><?= print_r($row['album_name'] . ' (' . $row['record_label'] .
') (' . $row['year_released'] . ')', true); ?><br><br></b>
<ul class="list-group list-group-flush">
<li class="list-group-item"><?= $row['name']?></li>
<?php
//get the rest of teh tracks
while($row = $result->fetch_assoc()) {?>
<li class="list-group-item"><?= $row['name']?></li>
<?php
} //end if While
?>
</ul>
</div>
</div>
</div>
This question is with reference to my previous question posted here PHP display results equivalent to comma separated values in the database.
I nearly solved the problem. Now I am stuck somewhere else which if solved my issue will be solved.
The problem is:
Please have a look at this code first
<div class="ads-container">
<?php
$cat = $pdo->prepare("SELECT * FROM ads_category");
$cat-> execute();
$i = 0;
while($s = $cat->fetch()){
$ads = $pdo->prepare("SELECT *, GROUP_CONCAT(memberships.mbs_color) FROM advertisements
INNER JOIN memberships ON FIND_IN_SET(memberships.mbs_id, advertisements.ad_memberships)
LEFT JOIN ads_category ON advertisements.ad_category = ads_category.ac_id
WHERE ad_credits >= ac_credits AND ad_category = :cat AND ad_status = 'active'
GROUP BY advertisements.ad_id");
$ads-> bindValue(':cat', $s['ac_id']);
$ads-> execute();
while($a = $ads->fetch()){ extract($a);
?>
<div class="" <?php if($i++ != 0){ echo "style='margin-top: 30px'"; } ?>>
<i class="fa fa-bullhorn" aria-hidden="true"></i> <?php echo $ac_category; ?>
</div>
<div class="col-sm-4">
<div class="adcover">
<div class="ad-title">
<?php echo $ad_title; ?>
</div>
<div class="ad-footer-two"> <?php echo $a['GROUP_CONCAT(memberships.mbs_color)']; // this is giving me the values comma separated perfectly ?>
<span class="membership-indicator" style="background: <?php echo $mbs_color; ?>; margin-top: 4px; margin-left: 5px"></span>
</div>
</div>
</div>
<?php } } ?>
</div>
Now the problem is when I echo <?php echo $a['GROUP_CONCAT(memberships.mbs_color)']; ?> I get the comma separed color values perfectly. But I want to display it as a color not as a word in <span class="membership-incdicator"></span> as exactly as in https://prnt.sc/hvj91s. So, how can I do that? Do I have to use foreach() function here? If yes then please tell me how to write that code. If not, then please tell me how to accomplish this.
This whole thing looks very messy, but explode the list and loop:
$mbs_colors = explode(',', $a['GROUP_CONCAT(memberships.mbs_color)']);
foreach($mbs_colors as $color) {
echo '<span class="membership-indicator" style="background: ' . $color . '; margin-top: 4px; margin-left: 5px"></span>';
}
Also, you can use an alias: SELECT *, GROUP_CONCAT(memberships.mbs_color) AS colors and then use: $a['colors'].
This does beg the question; why GROUP_CONCAT then? I'm not sure but there must be a better query.
I am selecting data from 3 tables using a two left joins.
This all works up until the iteration of one of the values for an association (i think, through process of elimination).
I now have reason to believe the select statement is incorrect at the point of the JOINs, but i cannot see how.
The join that is not functioning as expected:
LEFT JOIN
ae_template_pageTypes t ON t.ae_template_pageTypes_id = tp.ae_template_page_group_id
Here i am trying to get the ae_template_pageTypes_type_label by the ae_template_pageTypes_idassociation.
Table: ae_template_pages
Table: ae_template_pageTypes
Table: ae_template_groups
The HTML Output(Both showing HTML instead of using join respectively)
THE ISSUE
I am expecting 2 different values in the format section where here shows both as HTML. I cannot locate why this is happening.
The SELECT
SELECT tg.ae_template_group_name, tp.ae_template_page_id, tp.ae_template_page_group_id, tp.ae_template_page_title, tp.ae_template_page_type, tp.is_group_index, t.ae_template_pageTypes_id, t.ae_template_pageTypes_type_label
FROM
ae_template_pages tp
LEFT JOIN
ae_template_pageTypes t ON t.ae_template_pageTypes_id = tp.ae_template_page_group_id
LEFT JOIN
ae_template_groups tg ON tg.ae_template_group_id = tp.ae_template_page_group_id
WHERE tp.ae_template_page_group_id = '$tempGroup_id'
NOTE: I have tried all types of join to test without finding a solution.
The PHP
foreach ($template_pages as $key => $value) {
?>
<li class="dd-item dd3-item" data-id="<? echo $template_pages[$key]['ae_template_page_id']; ?>">
<div class="dd-handle dd3-handle"></div>
<div class="dd3-content ae_template_page" data-template_page_name='<? echo $template_pages[$key]['ae_template_page_title']; ?>' data-template_page_id='<? echo $template_pages[$key]['ae_template_page_id']; ?>'><? echo $template_pages[$key]['ae_template_page_title']; ?>
<span style='float: right;margin-top: -3px;'>
<div style="" class="btn-group">
<span class="btn-info btn-xs dropdown-toggle" data-toggle="dropdown" title="Click to change file type" type="button"><? echo $template_pages[$key]['ae_template_pageTypes_type_label']; ?></span>
<ul role="menu" class="dropdown-menu" data-page_id='<? echo $template_pages[$key]['ae_template_page_id']; ?>'>
<li>JS</li>
<li>HTML</li>
<li>CSS</li>
</ul>
<span class="btn-info btn-xs" title="This file belongs to Template Group: <? echo $template_pages[$key]['ae_template_group_name']; ?>" type="button"><? echo $template_pages[$key]['ae_template_group_name']; ?></span>
<?
if ($template_pages[$key]['is_group_index'] == 1) {
?>
<span class="btn-success btn-xs" title="This is the default file for this group" type="button">Group index</span>
<?
}
?>
</div>
</span>
</div>
</li>
<?
}
SQL workbench output using the above statement with the $tempGroupId set to 1
For this and future try and restructure your sql to make it clearer eg:
select * from (
(select * from table_a) tbla
left join
(select * from table_b) tblb
on tbla.commoncolname=tblb.commoncolname
)
Try this on the database tool first (eg SQL Workbench), then move on to what the php retrieves and so on to the html
please someone help me to find the solution for how to retrieve data from mysql database and populate in list view in jquery mobile.
am having the php code as follows
<? php
include('libraries/config.php');
$result = mysql_query("SELECT * from category") or die(mysql_error());
while ($obj = mysql_fetch_object($result)) {
$arr[] = $obj;
}
echo json_encode($data);
echo '{"Deals":'.json_encode($arr).'}';
?>
here am getting the data from mysql in json format but i was not known how to populate this in listview, my html page is as follows
<div id="content-area" style="height:auto;">
<br/>
<ul data-role="listview">
<li>
<a href="comfort_list.html">
<div class="content-home-tab1">
<div class="img-content">
<img id="ic_home" src="images/next_btn.png" style="margin-top:37px; margin-left:448px;" width="22" height="28" />
</div>
<div class="content-home-p">
<b>Comfort</b>
</div>
</div>
</a>
</li>
<li>
<a href="#">
<div class="content-home-tab1">
<div class="img-content">
<img id="ic_home" src="images/next_btn.png" style="margin-top:37px; margin-left:448px;" width="22" height="28" />
</div>
<div class="content-home-p">
<b>Handling Your Lenses</b>
</div>
</div>
</a>
</li>
</ul>
</div>
Here in my code am giving static data to list that is comfort and handling your lens, am having this data in db and i need to get that and to be placed here.
in the page i am posting in a form like this
<form method="post" action="help.html">
<ul data-role="listview" >
<?php while ($row = $stmt->fetch()){
?>
<li>
<a href="help1.php?id=<?php echo $row['categoryID']; ?>">
<div class="content-home-tab1">
<div class="img-content">
<img id="ic_home" src="images/next_btn.png" style="margin-top:37px; margin-left:448px;" width="22" height="28" />
</div>
<div class="content-home-p">
<b><?php echo $row['title'];?>
</b>
</div></div></a>
</li>
<?php }?>
</ul>
</form>
and in another page am trying to get the id as follows
<?php
include('libraries/config.php');
$getID = $_GET['id'];
echo $getID;
$stmt = $db->prepare("SELECT * from category where categoryID ='$_GET['id']'");
$stmt->execute();
?>
but when going to that page it is showing nothing and when refreshed then it is showing so that only am asking how to get it to fetch data and print it when the page is loaded. thanks.
As C.S. says, you don't need to use JSON, just loop through your array and add your list items.
Edited to show basic use of PDO, and demo how to access query results.
The mysql_query extension is deprecated as of PHP 5.5.0, so you I would suggest you use PDO_MySQL
First set up your database connection:
$dbName = "your_database_name_here";
$dbUserName = "your_username_here";
$dbPassword = "your_password_here";
$db = new PDO( 'mysql:host=localhost;dbname='.$dbName, $dbUserName, $dbPassword );
Then run your query, and echo your list (or whatever you need from the database by accessing the $row array within the while loop):
$stmt = $db->prepare("SELECT * from category");
$stmt->execute();
while ($row = $stmt->fetch()){
echo "<li>". $row['title'] ."</li>";
}
p.s. you should get rid of that inline css, it's a bad habit to get into.