PHP MYSQL match and output - php

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%')";

Related

Cannot Display multiple results with different links In PHP

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'].">

PHP display database using a search result

I am currently trying to create a search menu, and display it's result in a form of table. I managed to get the search result only as a string, but unable to use it to display a full row in the table.
Here is my code :
- Creating the Form :
<form action = "profile.php" method = "post">
<input type="text" name="search" placeholder = "Search">
<input type="submit" value = "SEARCH"><br>
</form>
This is the php to get search result
if(isset($_POST['search']))
{
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query("SELECT * FROM identity WHERE fullname LIKE '%$searchq%'") or die("Could not Find!");
$count = mysql_num_rows($query);
if($count == 0)
{
$output = 'There is no result';
}
else
{
while($row = mysql_fetch_array($query))
{
$fullname = $row['fullname'];
$output .= '<div> '.$fullname.' </div>';
$records = mysql_query("SELECT * FROM identitas_pengadu WHERE fullname='$output'");
}
}
}
This is the code I used to display the result in a database :
while($identity = mysql_fetch_assoc($records))
{
echo "<tr>";
echo "<td>".$identity['fullname']."</td>";
echo "<td>".$identity['nip']."</td>";
echo "<td>".$identity['email']."</td>";
echo "<td>".$identity['position']."</td>";
echo "<td>".$identity['status']."</td>";
echo "<td>EDIT</td>";
echo "</tr>";
}
It works fine if the value of $records does not use the WHERE function. Please help.
Thank You

Grabbing All of MYSQL Rows Using Array

Hi I'm trying to output all of the rows and information from my table :
id,Symbol,entry,exit,openclosed,entrydate,longshort,target_one,target_two,target_three,notes
This is through this script I'm working on to get this functionality. Right now I'm only outputting one of the database entries. This entry of course being the last one. For reference the last entry symbol is GLD. I'd like it to continue with the next symbols, but can't seem to get it to output. The outputted data for quote_0,quote_1 ect. come from yahoo as an array.
<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", 1);
//begin
include "storescripts/connect_to_mysql.php";
$sql = mysql_query("SELECT * FROM stockpicks ORDER BY id LIMIT 100");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
// get all the product details
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$symbol = $row["symbol"];
}
}
mysql_close();
//end
if(empty($symbol)) {
echo nothing;
}
else {
$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$symbol&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='leftofStocks'><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>";
}
?>
You have to more your output into the while loop to get access to each of the values in your table.
<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", 1);
//begin
include "storescripts/connect_to_mysql.php";
$sql = mysql_query("SELECT * FROM stockpicks ORDER BY id LIMIT 100");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
// get all the product details
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$symbol = $row["symbol"];
if(empty($symbol)) {
echo nothing;
}
else {
$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$symbol&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='leftofStocks'><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>";
}
}
}
mysql_close();
//end
?>
you should start using the msqli functions instead of mysql sinc eit is deprecated.
if everything else is correct in your code the only change you need to do is in the while clause.
while ($row = mysql_fetch_assoc($sql)){
$id[]=$row['id'];
$symbol[] =$row['symbol'];
}

if loop to bold certain records and keep others non-bold

I'm trying to get returned records from the database. I'm looking to bold returned questions (returned = 1) and non-bold questions (returned = 0) that haven't been returned for this page http://starsQA.com/Qbank
This is the query code so far:
<?php
include("db_conn.php");
$qry_string = "select * from stars order by starName ASC";
$prep = $pdo_conn->prepare($qry_string);
$prep->execute();
while ($row = $prep->fetch(PDO::FETCH_ASSOC)) {
echo "<button class='tosearch spcbtn' id='tglbtn{$row['starID']}'>{$row['starName']}</button>";
echo "<div style='clear:both;'></div>";
echo "<div style='color:black;' class='tgldiv' id='tgldiv{$row['starID']}'><b>{$row['starName']}</b><br><ol>";
$qry_stringq = "select * from questions where starID = ? && approved = 1";
$prepq = $pdo_conn->prepare($qry_stringq);
$prepq->execute(array($row['starID']));
while ($rowq = $prepq->fetch(PDO::FETCH_ASSOC)) {
echo "<li style='margin:30px'>{$rowq['question']}</li>";
}
echo "<ol'></div>";
echo "<div style='clear:both;'></div>";
}
?>
If you type in geno segers into the search box and open his button then questions 1-26 should be bold and question 27 should be non-bold
Maybe something like this?
while ($rowq = $prepq->fetch(PDO::FETCH_ASSOC)) {
$bold = $rowq['returned'] == 1 ? ' font-weight:bold;' : '';
echo "<li style='margin:30px;{$bold}'>{$rowq['question']}</li>";
}

check mysql database if checkbox is checked

i want to check if a checkbox is checked through a mysql database request.
it should be something like this (just a concept code, what of course isnt working)
that should work probably with ajax, becouse i dont want to reload all the time:
$ergebnis = $mysqli->query("SELECT text,status FROM checkboxes where id=1 ;");
while($zeile = $ergebnis->fetch_array()) {
echo "<input type=\"checkbox\";
if ({$zeile['status']} == "true") {checked=\"checked\"}\n ;
echo " name=\"feld\" class=\"checkIt\"/>";
echo " {$zeile['text']}\n";
echo "";
}
?>
i have got 3 fields in the database. One text field where the text next to the checkbox shows up, a status field, where the script can see if something is "true" or "false" an a auto incrementation id.
I hope you can help me
write this code
$ergebnis = $mysqli->query("SELECT text,status FROM checkboxes where id=1 ;");
while($zeile = $ergebnis->fetch_array()) {
$text = "";
$text .= "<input type=\"checkbox\"";
if ({$zeile['status']} == "true") { $text .= " checked=\"checked\""; }
$text .= " name=\"feld\" class=\"checkIt\"/>";
$text .= "{$zeile['text']}";
echo $text;
//echo "";
}
There are so many errors was present in your code.
$ergebnis = $mysqli->query("SELECT text,status FROM checkboxes where id=1 ;");
while($zeile = $ergebnis->fetch_array()) {
echo '<input type="checkbox"';
if ({$zeile['status']} == "true") { echo ' checked="checked"'; }
echo ' name="feld" class="checkIt" />';
echo $zeile['text'];
}
?>

Categories