$query = mysql_query("SELECT * FROM mailtbl WHERE fromuser = '$adminsess' AND sent = '1'");
echo "To Subject Date View message";
while($fetch = mysql_fetch_assoc($query)) {
$mid = $fetch['mid'];
$to = $fetch['touser'];
$subject2 = $fetch['subject'];
$message2 = $fetch['message'];
$date2 = $fetch['datesent'];
$rand = $fetch['rand'];
$view = "<a href = 'messages.php?mode=".$sent."&view=".$rand."'>View message</a>";
echo "$to$subject2$date2$view";
}
echo "</table>";
if (isset($view)) {
$viewget = $_GET['view'];
if ($viewget== $rand) {
echo "hi";
echo "$message2";
}
}
The $view there suppose to open the content of each message. The main problem is, if I have multiple values in the table, after clicking the link in each row in the table, the only link that is functioning is in the last row. The previous rows in the table with links doesn't show the content of the message (which is echo "hi" & echo "$message2";).
What exactly is wrong with my code? thanks.
Your code to display the message is outside the while loop. If you want it to run for every message, then put it in the loop that is iterating over every message.
The problem is that $rand is not set inside the isset($view) check. $rand still contains the last value from the while loop above, so only the last $viewget/$rand will work.
My guess is that messages.php shows all available messages (the message list) and if clicked on one message, the message list and that selected message.
So first, before the while loop, you fetch the message identifier of the message to be shown:
$ViewRequested = isset($_GET['view']) ? $_GET['view'] : -1;
$ViewRequestedData = NULL;
Next, inside the while loop, you check if the user is allowed to view the selected message, and remember the record:
if ($rand == $ViewRequested) {
$ViewRequestedData = $fetch;
}
Finally, after the while loop, you check whether there is a valid message requested:
if (is_array($ViewRequestedData) {
echo $ViewRequestedData['message2'];
}
Related
I have two HTML pages inside a session. On my first page, I want to create an array, which is saved in a $_SESSION variable. On the second page, I want to display my array.
PHP code for my first page looks like that:
<?php
include ("../script/db_connect.php");
$select_questions = 'select * from questions ';
if (isset($_POST["own"]) && $_POST["own"] == "No") {
$select_questions .= 'where creator != '
. $_SESSION["id"];
}
$select_questions .= 'limit 3';
$questions_result = mysqli_query($con, $select_questions);
$_SESSION["questions"] = mysqli_fetch_assoc($questions_result);
mysqli_close($con);
?>
PHP code on my second page looks like that:
<?php while ($array = $_SESSION["questions"]) {
echo $array["question"]; } ?>
When I go to second page, an infinite loop is displayed, where only the first element of my array is displayed over and over again. What is the reason for that? I cannot find any mistake in my code.
Change this
$_SESSION["questions"] = mysqli_fetch_assoc($questions_result);
to (here we fetch all rows from the sql result, not only one)
while($row=mysqli_fetch_assoc($questions_result)) $_SESSION["questions"][] = $row;
And later do
foreach($_SESSION["questions"] as $quest) { echo $quest["question"]; }
I am having some trouble with displaying some SQL query results.
Goal: I want to display the Helper 'name' in the table that is being generated if there is a helper signed up in the 'signup' table for that event 'eid' (event id).. If (1)there is no helper then display 'waiting for help', (2) there is a helper then display 'name -- awaiting approval..' and (3) else just display the name of helper..
Tried running the SQL query in phpMyAdmin with hard coded values and I get the results that I want so I know it is not my query. Have a suspicion that it is just the print out of the info into the table that is wrong somewhere. The table will display the data up until the ZIP from the address and then the next column which is the 'Helper' column does not display anything at all. So it makes me think I have a simple typo somewhere based on my if() statement logic BUT also find it interesting also that when I do the line:
echo "testing method -> ".getHelperIdOrName(2, 80)."<br>";
I cant get the table to print out at all. Not sure if this is related to my exact issue but it seems it could be. After I put this function in stuff stopped working so it seems like it could be culprit. The return of the function should either return an ID (int), a name "string", or just a generic value X (string)..
Any and all help is appreciated!
function getHelperIdOrName($x, $eid){
//Get the helper name first
$helperName = "";
$helperId = 0;
$sql = "SELECT id, first FROM users WHERE id IN (SELECT helper FROM signup WHERE gner = '".$userId."' AND eid = '".$eid."')";
$result = mysqli_query($db,$sql);
$row = $result->fetch_assoc();
if ($x == 2){
$helperName = $row["first"];
return $helperName;
}
else if ($x == 1){
$helperId = $row["id"];
return $helperId;
}
else {
return "X";
}
}
echo "testing method -> ".getHelperIdOrName(2, 80)."<br>";
//look for calendar and/or business approved events (approved=1) to display on page
$sql = "SELECT s.gner, s.helper, s.eid, s.approved, e.name, e.date, e.summary, e.street, e.city, e.state, e.zip
FROM signup s
INNER JOIN events e ON e.id = s.eid
INNER JOIN users u ON u.id = s.gner
WHERE s.gner = '".$userId."'";
$result = mysqli_query($db,$sql);
echo "<h3 class=\"text-center\">Events I'm Going To</h3>";
echo "<table class=\"table table-hover\"><tr><th>Event Name</th><th>Date</th><th>Summary</th><th>Location</th><th>Helper</th><th>Remove</th></tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["name"]."</td><td>".$row["date"]."</td><td>".$row["summary"]."</td><td>".$row["street"].", "
.$row["city"].", ".$row["state"]." ".$row["zip"]."</td>";
$tmp_eid = $row["eid"];
if (getHelperIdOrName(2, $temp_eid) == "X"){
echo "<td>Waiting for help..</td>";
}
else if ($row["approved"] == 0){
echo "<td>".getHelperIdOrName(2, $temp_eid)." -- Awaiting Approval (see below)</td>";
}
else {
echo "<td>".getHelperIdOrName(2, $temp_eid)."</td>";
}
echo "<td><form method=\"post\" action=\"remove.php\">
<button type=\"submit\" name=\"remove\" value=\"".$row["eid"]."\">Not Going</button></form></td></table>";
}
}
else echo "</table><br><p class=\"text-center\">You are not signed up for any events. Click here to sign up for events near you!</p>";
Thanks for that Jeff. The issue was that inside of the function it indeed did not know what $userId was even though I had the include statement at the top of my php file. I had to add this line into my function at the top..
global $db; //is part of my db my connection info in my config.php file
and then I also passed the $userId to the function as a parameter
these lines are what I used to help me see the errors:
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
i also had some ending < /table > tags inside some if logic so that fixed the funky displays I was getting (2nd row of table being outside of the table)
I have a for-each statement based off a php generated query..
I am trying to figure out how I can make sure all the IDDestinations of the records are the same.
For example in my current query I have 2 records with an IDDestination of 12, one record with an IDDestination of 9 and the last is 3.
I know how to do this in the query but I am trying to generate a message to the user if the IDDestinations are not equivalent.
My code so far.
foreach($results as $row) {
$IDDestination =(int) $row['IDDestination'];
if ($IDDestination == $IDDestination){
echo "<script>alert('All Courses Match Destination.');</script>";
} else {
echo "<script>alert('Courses have different Destinations);</script>";
}
var_dump($IDDestination);
}
This is currently just verifying that each record has an IDDestination Present and tells ME All courses Match.
How can I make it so the INTEGERS are equivalent and give me the same message?
Here's one way; use a variable outside your loop to determine if it's ok or not:
$everything_matches = true;
$id = null;
foreach($results as $row) {
// Set it for the first record.
if($id === null)
$id = $row['IDDestination'];
// If the current iteration's ID matches the first record, $everything_matches
// will stay true, otherwise it's false and we should kill the loop.
if($id != $row['IDDestination']) {
$everything_matches = false;
break;
}
}
// Output your message
$message = $everything_matches ? 'All courses match destination.' : 'Courses have different destinations.';
echo '<script>alert("' . $message . '");</script>';
I have a MySQL table that has various entries, some with video links, and some without. Let's call this row "vid" for now. I would like, if the row has a link (or any text at all), to set the link equal to the $pc162v variable, and if the row has no content for "vid" to set the variable to be blank.
Here's the variable declaration:
$pc162v = "";
$v162 = '<center><embed width="420" height="236" src="'.$pc162v.'" type="application/x-shockwave-flash"></embed></center><hr>';
Further down in the code, all under an "if" statement:
} else {
$i = 0;
while ($row = $result->fetch_row()) {
if ($row[7] != "") {
$pc162vid = $row[7];
}
else {
$vidspot162 = '';
}
The area for the video, even though the PHP is being pointed to the correct row, is blank. Any help/ideas is appreciated!
I have a page that will display the names of people from one data base that is joined with another that tracks when two names are linked. Every thing works fine. However; I cannot make the data or specifically the right user name ($id2u) and a string ($permissions) transfer from the input boxes when the user selects one. The onclick event should then show the data fields as per the permission string. The problem is the displayed info is always the last user in the DB that was ran through the loop. Am trying to get the info to carry forward on the onclick event when a check box is clicked so that their info is displayed.
This is he function to call the display of data.
function display1(type) {document.getElementById("pi").style.display = "none"
document.getElementById(type).style.display = ""}
This is the call to the DB that generated the input boxes for each person.
$result1 = mysql_query('SELECT * FROM level1 LEFT JOIN linkreq ON level1.idnumber = linkreq.idme ORDER BY LEAST(lname, fname) DESC');
for ($i = mysql_num_rows($result1) - 1; $i >= 0; $i--){
if (!mysql_data_seek($result1, $i)){echo "Cannot seek to row $i: " . mysql_error() . "\n";continue;}
if (!($row = mysql_fetch_assoc($result1))) {continue;}
if($_SESSION["idnumber"] == $row['id2u']){
if($row['returnack'] == 0){$id2u = $row['idme']; echo'<font color="#FF6600">*</font> '.$row['title'].' '.$row['fname'].' '.$row['mname'].' '.$row['lname'].' '.$row['suffex'].'<br />';}
else{
$permissions = $row['permissions'];
parse_str($permissions);
$permissions;
$id2u = $row['idme'].' ';
echo '<input type="checkbox" value = " .$id2u. " onClick="display1(\'pi\');"> '
.$row['title'].' '.$row['fname'].' '.$row['mname'].' '.$row['lname'].' '.$row['suffex'].'
<br />';}}}
This is the data that gets displayed onclick(pi) but displays last user not the selected one. Basically if the $id2u variable data would carry over from the check box this would work. As can manually force it by setting $id2u = xxxxxx; just before the query.
$result2 = mysql_query('SELECT * FROM pi WHERE idnumber = "'.$id2u.'"');
for ($i = mysql_num_rows($result2) - 1; $i >= 0; $i--){
if (!mysql_data_seek($result2, $i)){echo "Cannot seek to row $i: " . mysql_error() . "\n";continue;}
if (!($row = mysql_fetch_assoc($result2))) {continue;}
echo '<br>row #'.$row['idnumber'];
if($id2u == $row['idnumber']){echo'...// PRINTS OUT DATA //...}}
</span>
I know this should be in mySQLi but for now just need to get it to work will convert later.
Why don't you use a while() loop ?
$query = mysql_query("...");
while($row = mysql_fetch_array($query))
{
...
}
This always works fine for me :)