I need some PHP coding assistance in building an echo to display the results of a database query...(list friends of the logged-in user?) I have the majority of it built, but I don't know how to echo the results data and link it to his/her profile? Any help would be greatly appreciated...maybe a code snippet of how to write it out?
Here is my code:
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if ($numrows > 0){
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
array_push($my_friends, $row["user2"]);
array_push($my_friends, $row["user1"]);
}
//remove your id from array
$my_friends = array_diff($my_friends, array($u));
//reset the key values
$my_friends = array_values($my_friends);
mysqli_free_result($query);
} else {
echo "You have no friends.";
}
// Build My friends From Results
****this is where I need help with*****
if (array_key_exists('0', $my_friends)){
$sql = "SELECT user1, user2
FROM friends
WHERE (user1='$u' OR user2='$u')
AND accepted='1'";
$result = mysqli_query($db_conx, $sql);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$my_friends = "<a href='user.php?u=".$result."'>". $row['avatar']. ' '. $row['firstname']. ' ' .$row['lastname'] ."</a>"; }
}
?>
Here is my html code:
<?php
echo "<li>$my_friends</li>";
?>
You are using the $result variable in the html link. Replace it with $row['id'] or the row attribute you use as GET parameter to see a user's detailed page
Related
Noob alert here. I'm just (trying to) teaching myself PHP because I need it on my internship job. Now I am creating a survey and I am trying to use while loops to show the questions and their respective answers onto my XAMPP. I am using this code:
<?php
mysqli_select_db($conn, "surveyordina");
//code hier schrijven
$sql = "SELECT questions_body FROM survey_questions where subthema_id = 1";
$sql2 = "SELECT answer_body FROM survey_answers where answer_id = 1 or answer_id = 2 or answer_id = 3";
$result = mysqli_query($conn, $sql);
$result2 = mysqli_query($conn, $sql2);
if(mysqli_num_rows($result2) > 0){
while($row = mysqli_fetch_assoc($result)) {
echo "<br>" ."Vraag: <br>" . $row["questions_body"]. "<br>";
while($row_answer = mysqli_fetch_assoc($result2)) {
echo $row_answer["answer_body"]. "<br>";
}
}
}
else{
echo "No results";
}
Now the returning part of this code looks like this:
Vraag:
Is the organization aware of where all the personal data is stored? Be it on-site or in the cloud, hosted by the company or by a third party?
Yes
No
I don't know
Vraag:
Is the organization able to locate and find personal data of a particular data subject?
Vraag:
Does the organization have technology in place to return all personal data on a given data subject, given a single search from personnel?
Vraag:
testerino kekkerino
I am trying to get the Yes, No, and I don't know parts under each and every question but only seem to get it under one of the questions.
Is there a method to return the whole answer part under each question? If so, am I on the right path or am I doing something completely wrong?
Thanks in advance.
Once you hit the end of a result-set, mysqli_fetch_assoc() doesn't just keep cycling through. So you're hitting the end of the $result2 set within the first outer loop, and then that's it, there are no more results to fetch.
You should fetch all of the $result2 results outside of the $result loop, assign the results to a variable, and then loop through that array within the $result loop.
Something like:
if(mysqli_num_rows($result2) > 0){
$result2Set = mysqli_fetch_all($result2, MYSQLI_ASSOC);
while($row = mysqli_fetch_assoc($result)) {
echo "<br>" ."Vraag: <br>" . $row["questions_body"]. "<br>";
foreach($result2Set as $row_answer) {
echo $row_answer["answer_body"]. "<br>";
}
}
}
I want to make a login system, where the user can see all the messages he has received. In my database the insurance_company is the username and that is what I want it to show by. Here is my code. How do I fix it?
- Thanks Prem
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['Claim'] . "<br />";
}
?>
PHP code can be written anywhere in HTML code. You need to just place your PHP code between tags. As you can see in the example bellow I have used php code between p tags.
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {?>
<p> <?php echo $row['Claim'] . "<br />"; ?> <p>
<?php }
?>
If you want it to show insurance_company, you must echo this value in the response array:
echo $row['insurance_company']
i have an search box on website but whenever we search, it doesn't give any output.
I want to search for page title from database. But don't know what is wrong.
<div id="siteSearch">
<h3>Site Search</h3>
<?php
if (isset($_POST['search'])) {
$search = $_POST['search'];
$query = "SELECT * FROM pages WHERE ptitle LIKE '%$search%'";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
if ($count > 0) {
while ($fetch = mysql_fetch_array($result)) {
echo $fetch['ptitle'];
}
}
} else {
echo "No result found!";
}
?>
</div>
You are using the semicolon after the while loop:
while($fetch = mysql_fetch_array($result));
Please remove the semicolon in this line.
You have a misplaced closing bracket. The one above else should be 5 lines below. As it currently stands, the else clause relates to if (isset($_POST['search'])).
I have looked through similar problems and solution but somehow only half way help me with my problem. I'm trying to make a form to checked more than one record from MySQL database and display the checked record to another page. Somehow I managed to do the page with check boxes but I don't know how to display the record checked. It can only display the first row of the record or all the records regardless which box are checked.
This is checkbox page
$columns = count($fieldarray);
//run the query
$result = mysql_query(
"SELECT * FROM request_item
ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error());
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
{
$rows[] = $row['IllNo'];
}
foreach($rows as $value);
echo "";
echo " ";
echo $row['IllNo'];
echo "";
}
echo "";
?>
This is display record checked
$columns = count($fieldarray);
//run the query
$result = mysql_query(
"SELECT * FROM request_item
ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error());
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$rows[]=$row['IllNo'];
foreach($rows as $value);
if ($rows= 'checked') {
echo "";
echo $value;
}
Any help are welcome. Thank you.
There's actually a lot of problems with that script including syntax errors, calling the wrong variable name, form not opening where it should, invoking PHP after you already have, etc...
To get a good answer to you, you should share what make $row['IllNo'] should equal to indicate if it should be checked or not.
I reformatted it a bit and this may give you a good start.
<form NAME ="form1" METHOD ="POST" ACTION ="dari.php">
<table>
<?php
$columns = count($fieldarray);
//run the query
$result = mysql_query("SELECT * FROM request_item ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error()) ;
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result)) {
echo "<tr><td>";
echo "<Input type = 'Checkbox' Name ='ch1' value ='ch1'";
// check checked if it is. this will be checked if $row['IllNo'] has a value
// if there were a condition to make it checked, you would put the condition
// before the ?
echo $row['IllNo'] ? ' checked' : '';
echo ' />';
echo $row['IllNo'];
echo "</td></tr>";
}
?>
</table>
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Choose your books">
</FORM>
so as the title states, using the following code I have got it populating the dropbox with a single result from the query, that result being the latest added in the table.
here is my code:
<?php
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_assoc($result)){
$aa = "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $aa; ?></select>
The odd thing is, I use this same code for another field, and it works, populating the dropdown with all the results, however in this case it only fills in the last unit code in the table and not all of which are attached to the particular user id.
I would appreciate anyones thoughts :D
thanks
$aa .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
add . before = and initiate $aa = ''; before while loop
<?php
$query = "SELECT * FROM units_tb WHERE user_id='$userid'";
$result = mysql_query($query) or die (mysql_error());
$options = "";
while($row = mysql_fetch_assoc($result)){
$options .= "<option value='{$row['unit_id']}'>{$row['unit_code']}</option>";
}
?>
<select name="t_unit"><? echo $options; ?></select>
should work. You forgot a . in your while loop