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'];
}
Related
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%')";
#Evan trying to echo out 0 results on to the newpage.php also how can i define the $searchQuery from newpage.php... it seems like i have to rewrite all of the code onto the newpage.php ... is there a way to just to form action to another page?
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once("db_connects.php");
$queryArray = array();
$goodQuery = true;
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
$searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
{
$sqlCommand = "(SELECT id, links, page_body, page_title AS title FROM pages WHERE MATCH (page_title,page_body) AGAINST ('$searchquery'))";
}
$query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />";
while($row = mysql_fetch_assoc($query)){
$queryArray[] = $row;
}
}
else {
$goodQuery = false;
$_SESSION['error'] = true;
header("Location: newpage.php");
}
if($goodQuery){
$_SESSION['search_output'] = $queryArray;
header("Location: newpage.php");
exit;
}
else{
echo $search_output;
}
}
?>
This is the code on the newpage.php once it is header away..
<?php
session_start();
if(isset($_SESSION['error'])){
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
} else {
foreach($_SESSION['search_output'] as $value){
$value['links'];
$value['title'];
$value['page_body'];
$title = $value['title'];
$link = $value['links'];
$body = $value['page_body'];
$search_output .= "<a href='".$link."'>".$title."</a> - $body<br>";
}
}
?>
<div>
<?php echo $search_output; ?>
</div>
You're going to want to store the query results in an array. Once you have done that you can use header in php to bring the user to a new page. Once the user is on the new page you can echo back the results of the query (which is now stored in the $_SESSION):
First, create an array:
$queryArray = array();
Second, query the database and store each row in the array we just created
while($row = mysql_fetch_array($query)){
$queryArray[] = $row;
} // close while
Third, move the array to the SESSION variable:
$_SESSION['search_output'] = $queryArray;
Fourth, move the user to the new page:
header("Location: newpage.php");
Lastly, echo out your data:
foreach($_SESSION['search_output'] as $value){
echo $value;
}
EDIT: Update with full code:
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once("db_connects.php");
$queryArray = array();
$goodQuery = true;
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
$searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
$sqlCommand = "(SELECT id, links, page_body, page_title AS title FROM pages WHERE MATCH enter code here(page_title,page_body) AGAINST ('$searchquery'))";
}
$query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />";
while($row = mysql_fetch_array($query)){
$queryArray[] = $row;
}
}
else {
$goodQuery = false;
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
}
if($goodQuery){
$_SESSION['search_output'] = $queryArray;
header("Location: newpage.php");
exit;
}
else{
echo $search_output;
}
?>
Now, on the newpage.php:
You now need to get the results from your query (which are stored in the SESSION). You can do this by looping through the SESSION:
session_start(); // Make sure you add this at the top of the page
foreach($_SESSION['search_output'] as $value){
echo $value;
}
I am trying to make it to if I have at least 1 team created for one person, it will display the team. If they don't have a team, it will say no teams. It works if the person has at least 1 team, but nothing shows if the person is not on a team. How do I fix this?
<?php
$sql = mysql_query("SELECT * FROM teams WHERE players LIKE '%$sessiongamt%'") or die("Could not allocate information!");
$num = 0;
while($row = mysql_fetch_assoc($sql)){
$num = ++$num;
$amount1 = mysql_num_rows($sql);
$name = $row["name"];
$teamrank = $row["rank"];
$teamlink = $row["link"];
$players = $row["players"];
$teamid = $row['id'];
if($amount1 < 1){
$teams = "No Teams";
echo "$amount";
}else{
$teams = "$name";
echo "<a href='$teamlink?id=$teamid'>$teams</a>";
}
}print "$amount1";
?>
Look at the third line of this code - put it in your code on the same place instead of your line.
if($amount1 < 1){
$teams = "No Teams";
echo $teams;
}else{
$teams = "$name";
echo "<a href='$teamlink?id=$teamid'>$teams</a>";
}
Put the counting outside the while... loop
$sql = mysql_query("SELECT * FROM teams WHERE players LIKE '%$sessiongamt%'") or die("Could not allocate information!");
$amount1 = mysql_num_rows($sql); //<---- this should fix it
$num = 0;
while($row = mysql_fetch_assoc($sql)){
$num = ++$num;
$name = $row["name"];
$teamrank = $row["rank"];
$teamlink = $row["link"];
$players = $row["players"];
$teamid = $row['id'];
$teams = "$name";
echo "<a href='$teamlink?id=$teamid'>$teams</a>";
}
if($amount1 < 1){
$teams = "No Teams";
}
print "$amount1";
?>
I have a code which fetches data from a mysql table and converts it into pdf document, the code is working fine except it is skipping row 1.
Here is the code from which i have removed the pdf generation process since the problem is in the loop which is fetching data.
Please help.
<?php
session_start();
if(isset($_SESSION['user']))
{
$cr = $_POST['cour'];
$s = $_POST['sem'];
require('fpdf.php');
include('../includes/connection.php');
$sql = "SELECT * FROM `student` WHERE AppliedCourse ='$cr'";
$rs = mysql_query($sql) or die($sql. "<br/>".mysql_error());
if(!mysql_fetch_array($rs))
{
$_SESSION['db_error'] = "<h2><font color = 'RED'>No such course found! Pease select again.</font></h2>";
header('Location: prinrepo.php');
}
else {
for($i = 0;$i <= $row = mysql_fetch_array($rs);$i++)
{
$formno[$i] = $row ['FormNo'];
$rno[$i] = $row ['rollno'];
$snm[$i] = $row ['StudentNm'];
$fnm[$i] = $row ['FathersNm'];
$mnm[$i] = $row ['MothersNm'];
$addr[$i] = $row['Address'];
$pic[$i] = $row['imagenm'];
$comm[$i] = $row['SocialCat'];
echo $formno[$i]."<br />";
echo $rno[$i]."<br />";
echo $snm[$i]."<br />";
echo $fnm[$i]."<br />";
echo $mnm[$i]."<br />";
echo $addr[$i]."<br />";
echo $pic[$i]."<br />";
echo $comm[$i]."<br />";
echo "<br />";
}
}
mysql_close($con);
}
?>
You are fetching the first row outside of your for() loop then you miss it.
After mysql_query() your should use mysql_num_rows() to check if there are any rows in your result and then fetch them in the for loop.
More info here : http://php.net/manual/fr/function.mysql-num-rows.php
Your code would look like this :
$sql = "SELECT * FROM `student` WHERE AppliedCourse ='$cr'";
$rs = mysql_query($sql) or die($sql. "<br/>".mysql_error());
if(0 == mysql_num_rows($rs)) {
$_SESSION['db_error'] = "<h2><font color = 'RED'>No such course found! Pease select again.</font></h2>";
header('Location: prinrepo.php');
} else {
for($i = 0;$i <= $row = mysql_fetch_array($rs);$i++)
{
// Your code
}
}
When I run this query, it works fine but the individual fields would pull out. Like ThumbFilePath and Title.
If I run the query with only one field like:
$result = mysql_query("select ThumbFilePath from artwork where SCID = $SCID") or die(mysql_error());
It works fine. Any ideas why I can't pull the other fields?
<?php
$dbname = 'pdartist2';
$table = 'artowrk';
// query
$result = mysql_query("select AID, ThumbFilePath, Title, DisplayOrder from artwork where SCID = $SCID") or die(mysql_error());
while($row = mysql_fetch_row($result))
{
foreach($row as $cell)
{
echo "<div id='thumb_container'>";
echo "
<a href='gallery_detail.php?AID=$AID'>
<img src='http://markdinwiddie.com/PHP2012/$ThumbFilePath' title='Enlarge' alt='Enlarge' border='0'>
</a>
";
echo "$Title";
echo "</div>";
}
}
mysql_free_result($result);
?>
In general, you'll ALWAYS need to dereference the columns you need. For example:
while($row = mysql_fetch_row($result)) {
$aid = $row['AID'];
$tpath= $row['ThumbFilePath'];
$title = $row['title'];
...
<?php
$dbname = 'pdartist2';
$table = 'artowrk';
// query
$sql = "select AID, ThumbFilePath, Title, DisplayOrder from artwork where SCID = $SCID";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_row($result)) {
$AID = $row['AID'];
$ThumbFilePath= $row['ThumbFilePath'];
$Title = $row['Title'];
$DisplayOrder = $row['DisplayOrder'];
echo "<div id='clear'></div>";
echo "<div id='thumb_container'>";
echo "<a href='gallery_detail.php?AID=$AID'><img src='http://markdinwiddie.com/PHP2012/$ThumbFilePath' title='Enlarge' alt='Enlarge' border='0'></a>";
echo "<div id='name_spacer'></div>";
echo "<div id='thumbdesc'>";
echo "$Title";
echo "</div>";
echo "</div>";
}
mysql_free_result($result);
?>