Display current session name - php

I use this but only one will display...
please help me I need to display same name in a table..
<?php
$id=$_SESSION['SESS_MEMBER_ID'];
$result = mysql_query("SELECT * FROM user_reservation WHERE id = '$id' ");
while($row = mysql_fetch_array($result)){
echo '<tr class="record" id="'.$row['status'].'">';
echo '<td style="border-left: 1px solid #C1DAD7;">'.$row['confirmation'].'</td>';
echo '<td>'.$row['firstname'].' '.$row['lastname'].'</td>';
echo '<td><div align="right">'.$row['email'].'</div></td>';
echo '<td><div align="left">';
echo '</div></td>';
echo '<td><div align="right">'.$row['status'].'</div></td>';
echo '<td><div align="center">delete</div></td>';
echo '</tr>';
}
?>

Use session_start() at the top
<?php
session_start()
$id=$_SESSION['SESS_MEMBER_ID'];
$result = mysql_query("SELECT * FROM user_reservation WHERE id = '$id' ");
while($row = mysql_fetch_array($result)){
echo '<tr class="record" id="'.$row['status'].'">';
echo '<td style="border-left: 1px solid #C1DAD7;">'.$row['confirmation'].'</td>';
echo '<td>'.$row['firstname'].' '.$row['lastname'].'</td>';
echo '<td><div align="right">'.$row['email'].'</div></td>';
echo '<td><div align="left">';
echo '</div></td>';
echo '<td><div align="right">'.$row['status'].'</div></td>';
echo '<td><div align="center">delete</div></td>';
echo '</tr>';
}
?>

Related

ID'd link on click

I'm creating a contact based system, i have a contact list and want it to go to a full contact page when i click on a button, but the variable is being detected as a function?
<div id="po2" style="Margin:10% ">
<h1>Contacts</h1>
<?php
$sql = "SELECT * FROM `contacts`";
$query = mysqli_query($conn, $sql);
echo '<table class="data-table">';
echo'<thead>';
echo'<tr>';
echo '<th>Forename</th>';
echo '<th>Surname</th>';
echo '<th>Other</th>';
echo'</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_array($query))
{
echo '<tr>';
echo '<td>'.$row['Forename'].'</td>';
echo '<td>'.$row['Surname'].'</td>';
echo $var==$row['Forename']("<td><a href='View.php?ID= " . urlencode($var) ."'>
<button type='button'>link</button>
</a></td>");
echo '</tr>';
}
echo'</tbody>';
echo '</table>';
?>
</div>
You are using a comparison of $var and the $row. Try setting $var to the $row each loop iteration.
echo '<thead>';
echo '<tr>';
echo '<th>Forename</th>';
echo '<th>Surname</th>';
echo '<th>Other</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_array($query))
{
$var = $row['Forename'];
echo '<tr>';
echo '<td>' . $var . '</td>';
echo '<td>' . $row['Surname'] . '</td>';
echo "<td><a href='View.php?ID=" . urlencode($var) . "'><button type='button'>link</button></a></td>";
echo '</tr>';
}
echo '</tbody>';

Group rows from SQL database by date in tables

UPDATE - Added query
I am developing a system which gathers results from games from a MySQL database. I'd like the results to be 'grouped' by the date (e.g. results from a certain date will be in one table).
I'm nearly there, but struggling to get the results from the same date to display as the next row of the table.
.
Here's the code as it stands:
$result = mysqli_query($con, "SELECT `MatchDate`, t1.`TeamName` AS HomeTeam, `MatchHomeScore`, `MatchAwayScore`, t2.`TeamName` AS AwayTeam `FROM `match` INNER JOIN `team` t1 ON `match`.`MatchHomeTeamID` = t1.`TeamID` INNER JOIN `team` t2 ON `match`.`MatchAwayTeamID` = t2.`TeamID` ORDER BY `MatchDate` ASC ");`
$currentDate = false;
while($row = mysqli_fetch_array($result))
{
echo '<div class="col-lg-4">';
echo '<div class="panel panel-default-fixtures">';
if ($row['MatchDate'] != $currentDate){
echo '<div class="panel-heading">';
echo '<h4 class="panel-title">';
echo $row['MatchDate'];
echo '</h4>';
echo '</div>';
$currentDate = $row['MatchDate'];
}
echo '<table width="100%" class="table table-striped table-bordered table-hover dataTables">';
echo '<thead>';
echo '<tr>';
echo '<th class="all">';
echo 'Home';
echo '</th>';
echo '<th colspan="2">';
echo 'Score';
echo '</th>';
echo '<th class="all">';
echo 'Away';
echo '</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
echo '<tr>';
echo '<td>'.$row['HomeTeam'].'</td>';
echo '<td>'.$row['MatchHomeScore'].'</td>';
echo '<td>'.$row['MatchAwayScore'].'</td>';
echo '<td>'.$row['AwayTeam'].'</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
echo '</div>';
echo '</div>';
}
2nd update
Used the code Shamil Omarov provided and fixed the issue with a few changes. Here's the working code:
$result = mysqli_query($con, "
SELECT `MatchDate`, t1.`TeamName` AS HomeTeam, `MatchHomeScore`, `MatchAwayScore`, t2.`TeamName` AS AwayTeam
FROM `match`
INNER JOIN `team` t1 ON `match`.`MatchHomeTeamID` = t1.`TeamID`
INNER JOIN `team` t2 ON `match`.`MatchAwayTeamID` = t2.`TeamID`
-- WHERE YEAR(`MatchDate`) =2017
ORDER BY `MatchDate` ASC ");
$currentDate = false;
while($row = mysqli_fetch_array($result))
{
if ($row['MatchDate'] != $currentDate){
if ($currentDate != false){
echo '</tbody>';
echo '</table>';
// /.table-responsive
echo '</div>';
// /.panel
echo '</div>';
// /.col-lg-4
}
echo '<div class="col-lg-4">';
echo '<div class="panel panel-default-fixtures">';
echo '<div class="panel-heading">';
echo '<h4 class="panel-title">';
echo $row['MatchDate'];
echo '</h4>';
echo '</div>';
echo '<table width="100%" class="table table-striped table-bordered table-hover dataTables">';
echo '<thead>';
echo '<tr>';
echo '<th class="all">';
echo 'Home';
echo '</th>';
echo '<th colspan="2">';
echo 'Score';
echo '</th>';
echo '<th class="all">';
echo 'Away';
echo '</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
$currentDate = $row['MatchDate'];
}
if ($row['MatchHomeScore'] != null && $row['MatchAwayScore'] != null){
echo '<tr class="success">';
} else {
echo '<tr>';
}
echo '<td>'.$row['HomeTeam'].'</td>';
echo '<td>'.$row['MatchHomeScore'].'</td>';
echo '<td>'.$row['MatchAwayScore'].'</td>';
echo '<td>'.$row['AwayTeam'].'</td>';
echo '</tr>';
}
This new code now outputs:
check this.
$currentDate = false;
while($row = mysqli_fetch_array($result))
{
echo '<div class="col-lg-4">';
echo '<div class="panel panel-default-fixtures">';
if ($row['MatchDate'] != $currentDate){
if($currentDate != false){
echo '</tbody>';
echo '</table>';
echo '</div>';
echo '</div>';
}
echo '<div class="panel-heading">';
echo '<h4 class="panel-title">';
echo $row['MatchDate'];
echo '</h4>';
echo '</div>';
echo '<table width="100%" class="table table-striped table-bordered table-hover dataTables">';
echo '<thead>';
echo '<tr>';
echo '<th class="all">';
echo 'Home';
echo '</th>';
echo '<th colspan="2">';
echo 'Score';
echo '</th>';
echo '<th class="all">';
echo 'Away';
echo '</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
$currentDate = $row['MatchDate'];
}
echo '<tr>';
echo '<td>'.$row['HomeTeam'].'</td>';
echo '<td>'.$row['MatchHomeScore'].'</td>';
echo '<td>'.$row['MatchAwayScore'].'</td>';
echo '<td>'.$row['AwayTeam'].'</td>';
echo '</tr>';
}
I think you need to change the query. Could you show it in order to find out what is needed to be changed?

Checking and Returning values from database

I have this little issue:
I want to retrieve some rows from the database, it retrieves fine, but when i insert a particular value in the text field which is not present in the database to track, it returns an empty/blank page, below is the code i used:
<form id="track" name="track" method="post" action="track_now.php">
<h2>Track your shipment Here</h2>
<p><label> Tracking Reference:
<input type="text" id="reference" name="reference" value="" maxlength="40" required="required" /></label></p>
<div class="button_holder">
<p> <input type="submit" id="track" value="Track Now" maxlength="40" required="required" /></label>
</label></p>
</div>
</form>
and this is the track_now.php
<form id="track" name="track" method="post" action="">
<h2>Your Shipment Result</h2>
<?php
//error_reporting(0);
$ref = mysql_real_escape_string($_POST['reference']);
// conmnecting to the database
if(isset($ref))
{
$db = mysql_connect('localhost', 'admin', "admin") or die(mysql_error("Cannot Connect to Database"));
mysql_select_db('tracking') or die(mysql_error());
$sql = "SELECT * FROM order_tracking WHERE ship_ref = '".$ref."' ";
$rs = mysql_query($sql);
if($row = mysql_fetch_array($rs)) {
echo '<table width="518" border="1";>';
echo '<tr>';
echo '<td width="137" style="font-size:12px; padding: 5px;" >Shipment Reference: </td>';
echo '<td width="365" style="background-color:#fcfcfc; padding: 10px; font-size:12px;">' . $row['ship_ref'] . "<br />" . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td width="137" style="font-size:12px; padding: 5px;" >Shipment Type: </td>';
echo '<td width="365" style="background-color:#fcfcfc; padding: 10px; font-size:12px;">' . $row['ship_type'] . "<br />" . '</td>';
echo '</tr>';
}
echo "</table>";
}
else if ($rs != $row) {
print 'Invalid Tracking Number, Please click here to try again' ;
}
mysql_close();
?>
Please, what am i doing wrong here?
Your condition could be simplified, try this way:
if(isset($ref)){
$db = mysql_connect('localhost', 'admin', "admin") or die(mysql_error("Cannot Connect to Database"));
mysql_select_db('tracking') or die(mysql_error());
$sql = "SELECT * FROM order_tracking WHERE ship_ref = '".$ref."' ";
$rs = mysql_query($sql);
if(!rs){
die(mysql_error());
}
if($row = mysql_fetch_array($rs)) {
echo '<table width="518" border="1";>';
echo '<tr>';
echo '<td width="137" style="font-size:12px; padding: 5px;" >Shipment Reference: </td>';
echo '<td width="365" style="background-color:#fcfcfc; padding: 10px; font-size:12px;">' . $row['ship_ref'] . "<br />" . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td width="137" style="font-size:12px; padding: 5px;" >Shipment Type: </td>';
echo '<td width="365" style="background-color:#fcfcfc; padding: 10px; font-size:12px;">' . $row['ship_type'] . "<br />" . '</td>';
echo '</tr>';
echo "</table>";
}
mysql_close();
}
else{
print 'Invalid Tracking Number, Please click here to try again' ;
}
because else if ($rs != $row) will have undefined value if first condition is not met.
As #marco pointed out, you can check for rows without fetching:
if(mysql_num_rows($rs) > 0){
//found a row
}

Adding two fields in php

$result = mysql_query("SELECT * FROM productlist1 order by pdesc ASC");
while($row = mysql_fetch_array($result))
$sold=$row['psold'];
$left=$row['pleft'];
$all=$left + $sold;
{
echo '<tr>';
echo '<td>'.$row['pcode'].'</td>';
echo '<td>'.$row['pdesc'].'</td>';
echo '<td>'.$row['date'].'</td>';
echo '<td>'.$row['time'].'</td>';
echo '<td><div align="center">'.$row['psold'].'</div></td>';
echo '<td><div align="center">'.$row['pleft'].'</div></td>';
echo '<td><div align="center">'.$row['pprice'].'</div></td>';
echo '<td><div align="center">'.$all.'</div></td>';
i want to add the pleft and psold, the $all doesn't work at all... what would i do with this code?
You have a syntax error. You need to place following 3 lines inside brackets:
$sold=$row['psold'];
$left=$row['pleft'];
$all=$left + $sold;
like:
while($row = mysql_fetch_array($result))
{
$sold=$row['psold'];
$left=$row['pleft'];
$all=$left + $sold;
echo '<tr>';
....
You have to put opening braces for while loop just after the while statement like.
while($row = mysql_fetch_array($result))
{
$sold=$row['psold'];
$left=$row['pleft'];
$all=$left + $sold;
...............
Put your addition inside while loop,
because outside while loop it doesn't work, because it does not get proper value
So , try like this
<?php
$result = mysql_query("SELECT * FROM productlist1 order by pdesc ASC");
while($row = mysql_fetch_array($result))
{
$all=$row['psold'] + $row['pleft'];
echo '<tr>';
echo '<td>'.$row['pcode'].'</td>';
echo '<td>'.$row['pdesc'].'</td>';
echo '<td>'.$row['date'].'</td>';
echo '<td>'.$row['time'].'</td>';
echo '<td><div align="center">'.$row['psold'].'</div></td>';
echo '<td><div align="center">'.$row['pleft'].'</div></td>';
echo '<td><div align="center">'.$row['pprice'].'</div></td>';
echo '<td><div align="center">'.$all.'</div></td>';
Try this.
$result = mysql_query("SELECT * FROM productlist1 order by pdesc ASC");
while($row = mysql_fetch_array($result))
{
$sold=$row['psold'];
$left=$row['pleft'];
$all=$left + $sold;
echo '<tr>';
echo '<td>'.$row['pcode'].'</td>';
echo '<td>'.$row['pdesc'].'</td>';
echo '<td>'.$row['date'].'</td>';
echo '<td>'.$row['time'].'</td>';
echo '<td><div align="center">'.$row['psold'].'</div></td>';
echo '<td><div align="center">'.$row['pleft'].'</div></td>';
echo '<td><div align="center">'.$row['pprice'].'</div></td>';
echo '<td><div align="center">'.$all.'</div></td>';
echo '</tr>';
}
Try parsing the values to integer like
$all=intval($left)+intval($sold);
Yeah one more thing your while is misplaced!!

Combining several comments into one string

How could I combine all of the comments that appear in $row["comment"] below into one giant string variable called $commentstring?
$sqlStr = "SELECT comment.comment, comment.datecommented, comment.commentid, comment.points, login.username
FROM comment
LEFT JOIN login ON comment.loginid=login.loginid
WHERE submissionid=$submissionid
ORDER BY comment.points DESC
LIMIT 100";
$tzFrom1 = new DateTimeZone('America/New_York');
$tzTo1 = new DateTimeZone('America/Phoenix');
$result = mysql_query($sqlStr);
$arr = array();
echo "<table class=\"commentecho\">";
$count = 1;
while ($row = mysql_fetch_array($result)) {
$dt1 = new DateTime($row["datecommented"], $tzFrom1);
$dt1->setTimezone($tzTo1);
echo '<tr>';
echo '<td style="border-left:3px solid #DE2A00; background-color: #DE2A00; border-top:3px solid #DE2A00;" class="commentname2user">'.$row["username"].'</td>';
echo '<td style="border-bottom:3px solid #DE2A00; border-top:3px solid #DE2A00; border-right:3px solid #DE2A00;" rowspan="4" class="commentname1" id="comment-' . $row["commentid"] . '">'.stripslashes($row["comment"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td style="border-left:3px solid #DE2A00; background-color: #DE2A00;" class="commentname2">'.$dt1->format('F j, Y').'</td>';
echo '</tr>';
echo '<tr style="border-left:3px solid #DE2A00; background-color: #DE2A00; border-bottom:0px solid #DE2A00;">';
echo '<td style="border-left:3px solid #DE2A00;" class="commentname2"></td>';
echo '</tr>';
echo '<tr>';
echo '<td style="border-left:3px solid #DE2A00; background-color: #DE2A00; border-bottom:3px solid #DE2A00;" class="commentname2user"><span class="">'.number_format($row["points"]).'<span></td>';
echo '</tr>';
echo '<tr>';
echo '<td style="border-bottom:0px solid #DE2A00; border-right:0px solid #DE2A00;" class="commentname2a"></td>';
echo '</tr>';
}
echo "</table>";
Declare $commentstring = ""; before the while-loop
in the while loop:
$commentstring .= $row["comment"];
If you put this before your while loop:
$commentstring = '';
and then inside the while loop you add onto it:
$commentstring .= $row['comment'];
Or together with a new line:
$commentstring .= $row['comment'] . '<br />';

Categories