Hire Logo
Hire Logo

Reputation: 95

PHP MYSQL match and output

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>

Upvotes: 0

Views: 107

Answers (1)

DevZer0
DevZer0

Reputation: 13545

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

Upvotes: 1

Related Questions