I tried to use other ways but the result only displays one Link edit, since I have 3 kinds of Data/3 Persons, the edit links only shows to one person. I want to put 3 edit links with the same last name and will be updated later on the database.
$mysqli = new mysqli("localhost","root","","logindb");
$search = $mysqli->real_escape_string($_POST['search']);
$resultSet = $mysqli->query("SELECT * FROM precferm WHERE lname LIKE '$search%'");
if( $resultSet->num_rows > 0){
while($rows = $resultSet->fetch_assoc())
{
$lname = $rows ['lname'];
$fname = $rows ['fname'];
$mname = $rows ['mname'];
$output = " <a href=edit.php?id=$output> Edit</a><br /> Last Name: $lname<br />First Name: $fname<br />Middle Name: $mname<br /><br />";
}
}else{
$output = "No results";
}
}
It looks like you need to provide an id value for the URL:
if( $resultSet->num_rows > 0){
while($rows = $resultSet->fetch_assoc())
{
$id = $rows['id'];//something like this
$lname = $rows['lname'];
$fname = $rows['fname'];
$mname = $rows['mname'];
$output = " <a href=edit.php?id=$id> Edit</a><br /> Last Name: $lname<br />First Name: $fname<br />Middle Name: $mname<br /><br />";
}
}else{
$output = "No results";
}
}
the problem was that you were linking to a page with incorrect id, you should specify the link with a number to edit the row, I just added $id to route to correct page. good luck
if( $resultSet->num_rows > 0){
while($rows = $resultSet->fetch_assoc())
{
$lname = $rows ['lname'];
$fname = $rows ['fname'];
$mname = $rows ['mname'];
$id = $row['id'];
$output = " <a href='edit.php?id=$id'> Edit</a><br /> Last Name: $lname<br />First Name: $fname<br />Middle Name: $mname<br /><br />";
}
}else{
$output = "No results";
}
As stated by CBroe in comments:
You are overwriting $output in each loop iteration, so after the loop only the last value “survives”. You need to append to the variable (that you initialized with an empty string before the loop), if you want to get such links for all three records.
So:
You can use a PHP String operator to Append strings, making your string contain more data with each iteration of the loop.
I have also used "string".$var." more string" to illustrate the other type of string operator (concatenation) (as well as best practise for variables withi a string setting).
$output = ""; // ensure value starts empty.
if( $resultSet->num_rows > 0){
while($rows = $resultSet->fetch_assoc())
{
$lname = $rows ['lname'];
$fname = $rows ['fname'];
$mname = $rows ['mname'];
// note the .= appends data to the original $output
$output .= " Edit<br /> Last Name: ".$lname."<br />First Name: ".$fname."<br />Middle Name: ".$mname."<br /><br />";
}
}else{
// note the .= appends data to the original $output
$output .= "No results";
}
}
print $output // This will now output all itterations of the MySQL data loop.
NOTE
<a href=edit.php?id=".$output.">
This is adding the whole string ($output) to the id GET clause, and is incorrect. What you want here would be some sort of id counter; such as
<a href=edit.php?id=".$rows['id'].">
Related
Basically I'm doing digital signage and I'm trying to get names to be pulled from a MySQL database to a PHP page. Right now its all centered in one column, but I want the results to be in two columns side by side. How can I do this?
$sql = "SELECT * FROM donor WHERE DonationAmount = 5000 AND Category = '1' or DonationAmount = 5000 AND Category IS NULL ORDER BY LastName ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
// test if the DisplayName field is empty or not
if(empty($row['DisplayName']))
{
// it's empty!
if(empty($row['FirstName'])){
echo $row['LastName']. "<br>";
}
else{
echo $row["LastName"]. ", " . $row["FirstName"]. "<br>";
}
}else{
// Do stuff with the field
echo $row["DisplayName"]. "<br>";
}
}
} else {
}
Basically I want this data to be spread across two columns instead of 1 single page.
output the strings like this:
echo "<span style=\"width:50%;float:left;\">".$row['LastName']."</span>";
do not forget to remove <br /> from each output
You can use tables, and count the rows to determine if you need to start a new table row.
$i = 0;
$total_rows = $result->num_rows;
echo "<table><tr>";
while($row = mysqli_fetch_assoc($result)) {
// test if the DisplayName field is empty or not
echo "<td>";
if(empty($row['DisplayName']))
{
// it's empty!
if(empty($row['FirstName'])){
echo $row['LastName'];
}
else{
echo $row["LastName"]. ", " . $row["FirstName"];
}
}else{
// Do stuff with the field
echo $row["DisplayName"]. "";
}
echo "</td>";
$i++;
if($i % 2 == 0 && $i != $total_rows) {
echo "</tr><tr>";
}
}
echo "</tr></table>";
if your content is in <div id="myDiv"> use this JS function and call it after the content loads
function splitValues() {
var output = "";
var names = document.getElementById('myDiv').innerHTML.split("<br>");
for(var i in names) {
output += "<span style=\"width:50%;float:left;display:inline-block;text-align:center;\">"+names[i]+"</span>";
}
document.getElementById('myDiv').innerHTML = output;
}
How i separate the first result of for each loop and remaining. I have 2 divs, i want first result to be displayed there and rest on another div.
Also is there any way that i can get json decode without for each loop, i want to display result based on for each values from database, and querying database in for each loop is not recommended.
Here is my code, What i want
<div class="FirstDiv">
Result1
</div>
<div class="RemDiv">
Remaining result from for each loop
</div>
Here is full code
$data = json_decode($response->raw_body, true);
$i = 0;
foreach($data['photos'][0]['tags'][0]['uids'] as $value) {
if (++$i == 6)
break;
$check = "SELECT fullname FROM test_celebrities WHERE shortname = '$value[prediction]'";
$rs = mysqli_query($con,$check);
if (mysqli_num_rows($rs)==1) //uid found in the table
{
$row = mysqli_fetch_assoc($rs);
$fullname= $row['fullname'];
}
echo 'Celebrity Name: ' . $fullname . '<br/>';
echo 'Similar: ' . $value['confidence']*100 .'%'. '<br/><br/>';
echo "<img src='actors/$value[prediction].jpg'>";
echo "<hr/>";
}
Try this:
$data = json_decode($response->raw_body, true);
$i = 0;
echo '<div class="FirstDiv">'; // add this line here
foreach( $data['photos'][0]['tags'][0]['uids'] as $value ) {
if (++$i == 6) break;
$check = "SELECT fullname FROM test_celebrities WHERE shortname = '$value[prediction]'";
$rs = mysqli_query($con,$check);
if ( mysqli_num_rows($rs) == 1 ) { //uid found in the table
$row = mysqli_fetch_assoc($rs);
$fullname= $row['fullname'];
}
// Echo celebrity information:
echo 'Celebrity Name: ' . $fullname . '<br/>';
echo 'Similar: ' . $value['confidence']*100 .'%'. '<br/><br/>';
echo "<img src='actors/$value[prediction].jpg'>";
echo "<hr/>";
if ($i==1) { echo '</div><div class="RemDiv">'; }; // add this line here
}
echo '</div>'; // close the last tag
$predictions=array();
foreach($data['photos'][0]['tags'][0]['uids'] as $value) {
$predictions[]="'" . mysqli_real_escape_string($con, $value[prediction]) . "'";
}
$check="SELECT fullname FROM test_celebrities WHERE shortname IN (" . implode(',' $predictions) . ")";
$rs = mysqli_query($con,$check);
while ($row = mysqli_fetch_assoc($rs)) {
if (!$count++) {
// this is the first row
}
But note that you now have two sets of data which are sorted differently - hence you'll need to iterate through one and lookup values in the other.
I'm having a really hard time trying to figure out what I'm doing here. Let me explain first what I'm trying to do and what I'm getting so far.
What I have:
So far I have what I need in the way of grabbing data from yahoo to populate within a search. It is a simple form that asks for a visitor to input a symbol in which will then spit out quote information.
My goal:
I'm trying to correlate my database to also show my database information if the stock symbol is something I have information on. So say a visitor enters the stock symbol "hig" into the form I'd like to call my table 'stockpicks' to see if there is a match in the column 'symbol'. If this is the case i'd like to output other specific data from that table from other columns such as 'notes' for example.
Here is the active sample : http://www.stocksandstocks.com/stock-quotes.php
I can't seem to figure out the relationship between the two and how to get it right. Below is what I have so far.
<?php
error_reporting(E_ALL ^ E_NOTICE); //this is for debugging, remove if you dont need anymore
ini_set("display_errors", 1); //this is for debugging, remove if you dont need anymore
$searchoutput = "";
$ticker = "goog";
if (isset($_POST['get_quote'])) {
$ticker = $_POST['ticker'];
}
$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$ticker&f=sl1d1t1c1ohgv&e=.csv", "r");
$quote = fread($open, 1000);
fclose($open);
$quote = str_replace("\"", "", $quote);
$quote = explode(",", $quote);
$quote_0 = ($quote[0]);
$quote_1 = ($quote[1]);
$quote_2 = ($quote[2]);
$quote_3 = ($quote[3]);
$quote_4 = ($quote[4]);
$quote_5 = ($quote[5]);
$quote_6 = ($quote[6]);
$quote_7 = ($quote[7]);
$quote_8 = ($quote[8]);
echo "<div class='symbol'><div class='quote'>Company: $quote_0</div></div>";
echo "<div class='row'><div class='quote'>Last trade: $$quote_1</div>";
echo "<div class='quote'>Date: $quote_2</div>";
echo "<div class='quote'>Time: $quote_3</div>";
echo "<div class='quote'>From Previous: $$quote_4</div></div>";
echo "<div class='row'><div class='quote'>Open: $$quote_5</div>";
echo "<div class='quote'>High: $$quote_6</div>";
echo "<div class='quote'>Low: $$quote_7</div>";
echo "<div class='quote'>Volume: $quote_8</div></div>";
if (isset($_POST['get_quote']) && $_POST['get_quote'] != "") {
$ticker = $_POST['ticker'];
$get_quote = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['get_quote']);
$sqlCommand = "(SELECT id, symbol as sym FROM stockpicks WHERE symbol LIKE '%$get_quote%')";
include_once("storescripts/connect_to_mysql.php");
$query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$search_output .= "<hr />$count results for <strong>$get_quote/strong><hr />$sqlCommand<hr />";
while($row = mysql_fetch_array($query)){
$id = $row["id"];
$sym = $row["sym"];
$search_output .= "Item ID: $id - $sym<br />";
} // close while
} else {
$search_output = "<hr />0 results for <strong>$get_quote</strong><hr />$sqlCommand";
}
}
?>
<div class="form"> <form method="post" action="<?php echo $_SERVER['REQUEST_URI'];?>">
Get Quote: <input type="text" size="10" maxlength="10" name="ticker"/>
<input type="submit" value="Get Quote" name="get_quote" />
</form></div>
Enter any valid stock quote such as:<br>
aapl<br>
hog<br>
rimm<br>
rht<br>
<?php echo $search_output ;?>
</div>
You need to change your condition here if($count > 1){ to read if($count >= 1){ then you will be able to read the rows that are returned by the query.
second issue you having is a miss labeled variables. You need use the value of ticker, right now your using 'qet_quote' which is the name of a button.
$ticker = $_POST['ticker'];
$get_quote = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['ticker']);
$sqlCommand = "(SELECT id, symbol as sym FROM stockpicks WHERE symbol LIKE '%$get_quote%')";
For some reason I'm trying to display all of the members from the database in a list in order to access each of their profiles when I click on them, but I'm only getting the link of the last person in the database, any help?
include_once "../../mysql_server/connect_to_mysql.php";
//This code is used to display friends in the box of friends
$sql = mysql_query("SELECT * FROM myMembers");
$numberofRows = mysql_num_rows($sql);
$memberDisplayList = 'There are ' . $numberofRows .' members<br /><br />';
while($row = mysql_fetch_array($sql)) {
$id = $row['id'];
$firstname = $row["firstname"];
$lastname = $row["lastname"];
/////// Mechanism to Display Pic. See if they have uploaded a pic or not
$check_pic = "../../members/$id/image01.jpg";
$default_pic = "../../members/0/image01.jpg";
if (file_exists($check_pic)) {
$user_pic = "<img src=\"$check_pic?$cacheBuster\" width=\"80px\" />";
} else {
$user_pic = "<img src=\"$default_pic\" width=\"80px\" />";
}
$memberDisplayList = '' . $firstname .' '. $lastname .'<br />';
}
// ------- END WHILE LOOP FOR GETTING THE MEMBER DATA ---------
I think instead of
$memberDisplayList = '<a href= (...etc)
you meant to type
$memberDisplayList .= '<a href= (...etc)
which would append the new links to your string.
Also you don't seem to be echoing your $user_pic and $memberDisplayList strings anywhere.
Its because your overwriting the variables on each iteration, you need to hold the data within an array then do another foreach loop where ever you output:
<?php
while($row = mysql_fetch_array($sql)){
/////// Mechanism to Display Pic. See if they have uploaded a pic or not //////////////////////////
$check_pic = "../../members/{$row['id']}/image01.jpg";
$default_pic = "../../members/0/image01.jpg";
if (file_exists($check_pic)) {
$user_pic = "<img src=\"$check_pic?$cacheBuster\" width=\"80px\" />";
} else {
$user_pic = "<img src=\"$default_pic\" width=\"80px\" />";
}
$user[] = array('id'=>$row['id'],
'firstname'=>$row["firstname"],
'lasname'=>$row["lastname"],
'user_pic'=>$user_pic,
'display_list'=>'' . $row["firstname"] .' '. $row["lastname"] .'<br />');
}
?>
Where are you actually constructing the HTML? You're setting a bunch of variables in the code you presented, and that looks okay for what it is. So it's probably in the presentation logic. If that's in the loop, you're in good shape. But if it's outside of the loop, then I can't imagine where you'd ever display anything but the last row.
I am working on a lead management system - and as the database for it grows the need for more bulk functions appears - and unfortunately I am getting stuck with one of them. The database stores many different leads - with each lead being assigned to a specific closer; thus the database stores for each lead the lead id, name, closer name, and other info. The main lead list shows a checkbox next to each lead which submits the lead id into an array:
<input type=\"checkbox\" name=\"multipleassign[]\" value=\"$id\" />
Now this all goes to the following page:
<?php
include_once"config.php";
$id = $_POST['multipleassign'];
$id_sql = implode(",", $id);
$list = "'". implode("', '", $id) ."'";
$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$closer = mysql_result($result,$i,"business_name");
$businessname = mysql_result($result,$i,"closer");
echo "$closer - $businessname";
echo"<br>";
++$i; } } else { echo "The database is empty"; };
echo "<select name=\"closer\" id=\"closer\">";
$query2 = "SELECT * FROM members ";
$result2 = mysql_query($query2);
$num2 = mysql_num_rows ($result2);
if ($num2 > 0 ) {
$i2=0;
while ($i2 < $num2) {
$username = mysql_result($result2,$i2,"username");
$fullname = mysql_result($result2,$i2,"name");
echo "<option value=\"$fullname\">$fullname</option>";
++$i2; } } else { echo "The database is empty"; }
echo "</select>";
?>
I want to be able to use the form on this page to select a closer from the database - and then assign that closer to each of the leads that have been selected. Here is where I have no idea how to continue.
Actually - i got it. I don't know why I didn't think of it sooner. First off I passed the original $list variable over to the new page - and then:
<?php
include_once"config.php";
$ids = $_POST['list'];
$closer = $_POST['closer'];
$query = "UPDATE `promises` SET `closer` = '$closer' WHERE id IN ($ids) ";
mysql_query($query) or die ('Error updating closers' . mysql_error());
echo "A new closer ($closer) was assigned to the following accounts:";
$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$businessname = mysql_result($result,$i,"business_name");
echo "<li>$businessname";
++$i; } } else { echo "The database is empty"; };
?>
The updated page before this:
$query = "SELECT * FROM promises WHERE id IN ($list) ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$closer = mysql_result($result,$i,"business_name");
$businessname = mysql_result($result,$i,"closer");
echo "$closer - $businessname";
echo"<br>";
++$i; } } else { echo "The database is empty"; };
echo "<form name=\"form1\" method=\"post\" action=\"multiple_assign2.php\">";
echo "<input type=\"hidden\" name=\"list\" value=\"$list\" />";
echo "<select name=\"closer\" id=\"closer\">";
$query2 = "SELECT * FROM members ";
$result2 = mysql_query($query2);
$num2 = mysql_num_rows ($result2);
if ($num2 > 0 ) {
$i2=0;
while ($i2 < $num2) {
$username = mysql_result($result2,$i2,"username");
$fullname = mysql_result($result2,$i2,"name");
echo "<option value=\"$fullname\">$fullname</option>";
++$i2; } } else { echo "The database is empty"; }
echo "</select>";
echo "<input name=\"submit\" type=\"submit\" id=\"submit\" value=\"Reassign Selected Leads\">";
?>
After you select the leads and submit the form , your script should show them in a list with hidden inputs (with name=leads[] and value=the_lead's_id) and next to each lead there will be a dropdown box () which will be populated with all the closers.
After choosing and sending the second form your script will "run" all-over the leads' ids array and update each and every one of them.
Got the idea or you want some code?