Jump to content

basic if/else php code...need a little help


suprsnipes

Recommended Posts

Could someone be so kind as to help me get this bit of code working?

 

My PHP experience is only a couple of weeks old.

 

<?php
// Connect to MySQL
$con = mysql_connect("","","");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
  }

// Select database
mysql_select_db("spi", $con);

// query 1
$sql="SELECT * FROM trades2";	
if ($result=mysql_query($sql)) {

// Classification of trades
switch($result) {
case 'trd':
if ($type == "N") {

	if ($type1 == "a" and $price >= $price1) {
		echo 'buy';
	}
	else if ($type1 == "b" and $price <= $price1) {
		echo 'sell';
	}
	else {
		echo '';
}
?>

Link to comment
Share on other sites

Try the following instead

<?php 
// DATABASE CONNECTIVITY 
//----------------------------------------------------------------------------------------------------------- 
  $dbserver = "127.0.0.1";
  $dbusername = "";
  $dbpassword = "";  
  $dbname ="spi";
  $link=mysql_connect ($dbserver,$dbusername,$dbpassword) or die ('DB ERROR -> ' . mysql_error());
  mysql_select_db ($dbname); 
//--------------------------------------------------------------------------------------------------------- 
?>
<?php

$sql = "SELECT * FROM `trades2`";
$result2 = mysql_db_query($dbname,$sql,$link) or die("Invalid Query<br>". mysql_error());
while ($result = mysql_fetch_array($result2))
{
  switch($result[trade])
  {
    case 'trd':
    if(strtoupper($result[type])=='N')
    {
      if(strtolower($result[type1]) == 'a' && $result[price] >= $result[price1]){echo "Buy";}
      if(strtolower($result[type1]) == 'b' && $result[price] <= $result[price1]){echo "Sell";}
    }
  }
}

 

I'm presuming your db structure :)

Link to comment
Share on other sites

Hi dawsba,

 

Just run the suggested code and I'm getting the following error message;

 

Notice: Use of undefined constant trade - assumed 'trade' in www\Classification_CodeTest.php on line 18

 

Notice: Undefined index: trade in \www\Classification_CodeTest.php on line 18

 

Thanks for the reply by the way.

Link to comment
Share on other sites

Ok, I've updated the code but I still get this error message;

 

Notice: Undefined index: trade in C:\......\CodeTest.php on line 18

 

<?php 
// DATABASE CONNECTIVITY 
//----------------------------------------------------------------------------------------------------------- 
  $dbserver = "";
  $dbusername = "";
  $dbpassword = "";  
  $dbname ="spi";
  $link=mysql_connect ($dbserver,$dbusername,$dbpassword) or die ('DB ERROR -> ' . mysql_error());
  mysql_select_db ($dbname); 
//--------------------------------------------------------------------------------------------------------- 
?>
<?php

$sql = "SELECT * FROM `trades2`";
$result2 = mysql_db_query($dbname,$sql,$link) or die("Invalid Query<br>". mysql_error());
while ($result = mysql_fetch_array($result2))
{
  switch($result['trade'])
  {
    case 'trd':
    if(strtoupper($result['type'])=='N')
    {
      if(strtolower($result['type1']) == 'a' && $result['price'] >= $result['price1']){echo "Buy";}
      if(strtolower($result['type1']) == 'b' && $result['price'] <= $result['price1']){echo "Sell";}
    }
  }
}
?>

Link to comment
Share on other sites

Are you sure you have a field named trade in your table?

 

This was the problem I have attached the updated code  ;) The name of the field is `TYPE`. Now it appears that the code works as there are no error messages...what steps would I need to display the results?

 

<?php 

// DATABASE CONNECTIVITY 
//----------------------------------------------------------------------------------------------------------- 
  $dbserver = "";
  $dbusername = "";
  $dbpassword = "";  
  $dbname ="spi";
  $link=mysql_connect ($dbserver,$dbusername,$dbpassword) or die ('DB ERROR -> ' . mysql_error());
  mysql_select_db ($dbname); 
//--------------------------------------------------------------------------------------------------------- 

$sql = "SELECT * FROM `trades2`";
$result2 = mysql_db_query($dbname,$sql,$link) or die("Invalid Query<br>". mysql_error());
while ($result = mysql_fetch_array($result2))
{
  switch($result['TYPE'])
  {
    case 'trd':
    if(strtoupper($result['TYPE'])=='N')
    {
      if(strtolower($result['TYPE1']) == 'a' && $result['PRICE'] >= $result['PRICE1']){echo "buy";}
      if(strtolower($result['TYPE1']) == 'b' && $result['PRICE'] <= $result['PRICE1']){echo "sell";}
    }
  }
}
?>

 

 

Link to comment
Share on other sites

My apologies to the more experienced members but I am learning slowly but surely. I have noted next to the code below what results I'm trying to echo...I'm getting a blank screen with this code at the moment.

 

<?php 

// DATABASE CONNECTIVITY 
//----------------------------------------------------------------------------------------------------------- 
  $dbserver = "";
  $dbusername = "";
  $dbpassword = "";  
  $dbname ="spi";
  $link=mysql_connect ($dbserver,$dbusername,$dbpassword) or die ('DB ERROR -> ' . mysql_error());
  mysql_select_db ($dbname); 
//--------------------------------------------------------------------------------------------------------- 

$sql = "SELECT * FROM `trades2`";
$result2 = mysql_db_query($dbname,$sql,$link) or die("Invalid Query<br>". mysql_error());
while ($result = mysql_fetch_array($result2))
{
  switch($result['TYPE'])
  {
    case 'trd':
    if(strtoupper($result['TYPE'])=='N')
    {
      if(strtolower($result['TYPE1']) == 'a' && $result['PRICE'] >= $result['PRICE1']){echo "buy";}
      if(strtolower($result['TYPE1']) == 'b' && $result['PRICE'] <= $result['PRICE1']){echo "sell";}
    }
  }
}
?>

 

  if(strtoupper($result['TYPE'])=='N')

    {

      if(strtolower($result['TYPE1']) == 'a' && $result['PRICE'] >= $result['PRICE1']){echo "buy";} I want to echo this result

      if(strtolower($result['TYPE1']) == 'b' && $result['PRICE'] <= $result['PRICE1']){echo "sell";} & this result

Link to comment
Share on other sites

How do you mean?

 

I thought both of those statements were treated equally? If not I am mistaken.

 

The table that I have selected (trades2) has data that meets both criteria. All type columns data is equal to 'N' and I'm trying to classify the data with the code as described and I thought the echo would show a result on the screen with a "buy" or "sell"  where trades meet the criteria...

 

Am I on the wrong track here?

 

 

Link to comment
Share on other sites

I have also tried the following; The only error I get at the moment is as follows;

 

Parse error: parse error in C:\Program Files\EasyPHP 3.0\www\Needs work\Classification_CodeTest1.php on line 31

 

<?php 

// DATABASE CONNECTIVITY 
//----------------------------------------------------------------------------------------------------------- 
  $dbserver = "";
  $dbusername = "";
  $dbpassword = "";  
  $dbname ="spi";
  $link=mysql_connect ($dbserver,$dbusername,$dbpassword) or die ('DB ERROR -> ' . mysql_error());
  mysql_select_db ($dbname); 
//--------------------------------------------------------------------------------------------------------- 

$sql = mysql_query("SELECT * FROM `trades2`");
while ($result = mysql_fetch_array($sql))
{
  switch($result['TYPE'])
  {
    case 'trd':
    if ('TYPE'=='N'){

      	if ('TYPE1' =='a' && 'PRICE' >= 'PRICE1'){
  		echo "buy";
		break;
	}
      	else if ('TYPE1' =='b' && 'PRICE' <= 'PRICE1'){
  		echo "sell";
		break;
	}
}
}
?>

 

I'm not quite sure how to move forward with this. I've tried to cut it down to the bare minumum and I still can't make any progress. Basically my thought process is I'm trying to run the sql query to get the data first and then I want to run an if statement on the query and display the output of the result to screen. Where am I going wrong?

 

This is the SQL query; SELECT * FROM `trades2`

 

I want to run the following if statements on the data from the above SQL query and have the result output to screen...

 

   if ('TYPE'=='N'){

      	if ('TYPE1' =='a' && 'PRICE' >= 'PRICE1'){
  		echo "buy";
		break;
	}
      	else if ('TYPE1' =='b' && 'PRICE' <= 'PRICE1'){
  		echo "sell";
		break;
	}

 

This has been driving me banana's I've been able to do almost everything else I this is one of the last bits of code I need...

Link to comment
Share on other sites

Please help I have been stuck on this for weeks...

 

I can run other queries without too much of a problem, but an if statement I'm finding much much more difficult...I don't know how to go about it, can it even be done? I want to run my SELECT query first and from the result of the query run the if statement. But where do I put the if statement? And how do I have the results displayed on the screen?

 

I'm so frustrated... :'(

Link to comment
Share on other sites

If you could run the following it may help me understand your db structure: and display/post the results . :)

<?php

// DATABASE CONNECTIVITY 
//----------------------------------------------------------------------------------------------------------- 
  $dbserver = "";
  $dbusername = "";
  $dbpassword = "";  
  $dbname ="spi";
  $link=mysql_connect ($dbserver,$dbusername,$dbpassword) or die ('DB ERROR -> ' . mysql_error());
  mysql_select_db ($dbname); 
//--------------------------------------------------------------------------------------------------------- 

$sql = "SELECT * FROM `trades2`";
$result2 = mysql_db_query($dbname,$sql,$link) or die("Invalid Query<br>". mysql_error());
while ($result = mysql_fetch_array($result2))
{
  foreach($result as $k=>$v){echo $k." <-> ".$v ."<BR />";}
}
?>

Link to comment
Share on other sites

Results as requested...it's only a small sample, there are around 2600 records in the table.

 

0 <-> 18

ID1 <-> 18

1 <-> A

SYMBOL1 <-> A

2 <-> 27/05/2009 2:30:37 PM

DATE1 <-> 27/05/2009 2:30:37 PM

3 <-> 1430

TIME1 <-> 1430

4 <-> b

TYPE1 <-> b

5 <-> 3824

PRICE1 <-> 3824

6 <-> 8

SIZE1 <-> 8

7 <-> 19

ID <-> 19

8 <-> A

SYMBOL <-> A

9 <-> 27/05/2009 2:30:37 PM

DATE <-> 27/05/2009 2:30:37 PM

10 <-> 1430

TIME <-> 1430

11 <-> N

TYPE <-> N

12 <-> 3825

PRICE <-> 3825

13 <-> 1

SIZE <-> 1

0 <-> 19

ID1 <-> 19

1 <-> A

SYMBOL1 <-> A

2 <-> 27/05/2009 2:30:37 PM

DATE1 <-> 27/05/2009 2:30:37 PM

3 <-> 1430

TIME1 <-> 1430

4 <-> N

TYPE1 <-> N

5 <-> 3825

PRICE1 <-> 3825

6 <-> 1

SIZE1 <-> 1

7 <-> 20

ID <-> 20

8 <-> A

SYMBOL <-> A

9 <-> 27/05/2009 2:30:37 PM

DATE <-> 27/05/2009 2:30:37 PM

10 <-> 1430

TIME <-> 1430

11 <-> N

TYPE <-> N

12 <-> 3825

PRICE <-> 3825

13 <-> 1

SIZE <-> 1

0 <-> 22

ID1 <-> 22

1 <-> A

SYMBOL1 <-> A

2 <-> 27/05/2009 2:30:37 PM

DATE1 <-> 27/05/2009 2:30:37 PM

3 <-> 1430

TIME1 <-> 1430

4 <-> b

TYPE1 <-> b

5 <-> 3824

PRICE1 <-> 3824

6 <-> 10

SIZE1 <-> 10

7 <-> 23

ID <-> 23

8 <-> A

SYMBOL <-> A

9 <-> 27/05/2009 2:30:38 PM

DATE <-> 27/05/2009 2:30:38 PM

10 <-> 1430

TIME <-> 1430

11 <-> N

TYPE <-> N

12 <-> 3825

PRICE <-> 3825

13 <-> 1

SIZE <-> 1

0 <-> 25

ID1 <-> 25

1 <-> A

SYMBOL1 <-> A

2 <-> 27/05/2009 2:30:38 PM

DATE1 <-> 27/05/2009 2:30:38 PM

3 <-> 1430

TIME1 <-> 1430

4 <-> a

TYPE1 <-> a

5 <-> 3825

PRICE1 <-> 3825

6 <-> 3

SIZE1 <-> 3

7 <-> 26

ID <-> 26

8 <-> A

SYMBOL <-> A

9 <-> 27/05/2009 2:30:38 PM

DATE <-> 27/05/2009 2:30:38 PM

10 <-> 1430

TIME <-> 1430

11 <-> N

TYPE <-> N

12 <-> 3825

PRICE <-> 3825

13 <-> 1

SIZE <-> 1

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.