CodingA database for my data

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  ANT_THOMAS   
 To:  steve     
35356.101 In reply to 35356.100 
code:
Parse error: syntax error, unexpected T_ELSE in C:\wamp\www\full-iftest.php on line 41


After adding a } it still doesn't like it. Clearly has an issue with the else statement.

0/0
 Reply   Quote More 

 From:  dave (10_ROGUE)  
 To:  ANT_THOMAS      
35356.102 In reply to 35356.101 

What about the missing { before the else?

 

and by before I do ofcourse mean after.

0/0
 Reply   Quote More 

 From:  empathy  
 To:  ANT_THOMAS      
35356.103 In reply to 35356.101 
missing semicolon?
0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  empathy     
35356.104 In reply to 35356.103 
Possibly that! It works now :)

0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  ALL
35356.105 

Yay, another question, and most likely an easy one to answer. I am trying to make my thingy easier to make changes to in the future and because I am currently doing things in a retarded manner I want to change things.

 

I currently have a few separate pages/files to show entries in certain ways

 

full.php - standard single entry based on id
full5.php - as above but 5 entries
fullc.php - pulls single entry based on code
fullr.php - pulls single entry based on lab book ref

 

I realise this is very much the wrong way to do things, hence why I want to change it.

 

I currently decide things things based on using $_GET

 

.....php?id=xxx
.....php?code=ATxxx
.....php?ref=ATrxxx

 

I have easily got rid of the one to pull 5 entries so that's not a problem. I want to now combine all the others so I can for example use any of these:

 

full.php?id=xxx
full.php?code=ATxxx
full.php?ref=ATrxxx

 

I use these to get the values:

 

$tableid = $_GET['id'];
$tablecode = $_GET['code'];
$ref = $_GET['ref'];

 

But how do I now tell it to fetch the record based on which is actually present?

 

There will only ever be one of those there at any one time. I assume using some sort of if else null statements would sort it but I'm not sure how to go about it.


0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  ALL
35356.106 
Right, I think I may be somewhere along the right lines but it doesn't work...

I have :

PHP code:
 
$get = $_GET['get'];
 
if $get == ('AT###')
{$query  = "SELECT * FROM nmr WHERE code = $ref LIMIT $ent ";}
 
else if $get == ("ATr###")
{$query = "SELECT * FROM nmr WHERE labbookref = $ref ";}
 
else $get == ("###") 
{$query = "SELECT * FROM nmr WHERE id >= $ref LIMIT $ent ";}
 


$ent is just the number of entries to fetch.

$get is either going to be either of these

AT### - (code)
ATr### - (labbookref)
### - (id)

How? :C

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  ANT_THOMAS      
35356.107 In reply to 35356.106 
You can use a regular expression to identify things.


Not sure if this is exactly right PHP syntax, but something along these lines should work...

php code:
preg_match ( /^(ATr?)?(.*)$/ , $get , $groups );
 
$ref= $groups[2];
 
$query = "SELECT * FROM nmr ";
 
switch( $groups[1] )
{
 
	case 'AT':
		$query .= "WHERE code = $ref";
	break;
 
	case 'ATr':
		$query .= "WHERE labbookref = $ref";	
	break;
 
	default:
		$query .= "WHERE id >= $ref";
	break;
}
 
$query .= "LIMIT $ent";
 
0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  Peter (BOUGHTONP)     
35356.108 In reply to 35356.107 
Thank you for the reply.

I've managed to get it to work using what is probably an unorthadox way but it works :D


PHP code:
$get = $_GET['get'];
$ent= $_GET['ent']+1;
$grab = $_GET['grab'];
 
 
if ($get == "a")
{$query = "SELECT * FROM nmr WHERE id >= '$grab' LIMIT $ent ";}
 
if ($get == "b")
{$query  = "SELECT * FROM nmr WHERE code = '$grab' LIMIT $ent ";}
 
if ($get == "c")
{$query = "SELECT * FROM nmr WHERE labbookref = '$grab' ";}


Using a link along the lines of:

http://server/full.php?get=a&grab=25&ent=5

0/0
 Reply   Quote More 

 From:  Dan (HERMAND)  
 To:  ANT_THOMAS      
35356.109 In reply to 35356.108 
Beware of putting input directly into SQL queries.
0/0
 Reply   Quote More 

 From:  koswix  
 To:  ALL
35356.110 
*spooky music & lightning*


The Seventh Posture of Burton's translation of The Perfumed Garden is an unusual position not described in other classical sex manuals. The receiving partner lies on their side. The penetrating partner faces the receiver, straddling the receiver's lower leg, and lifts the receiver's upper leg on either side of the body onto the crook of penetrating partner's elbow or onto the shoulder. While some references describe this position as being "for acrobats and not to be taken seriously," others have found it very comfortable, especially during pregnancy.
0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  ALL
35356.111 
Yay, more of me coding and being shit at it.

Simple one (I think)

I have a database with this table..

date day month year dayofweek weight
2010-08-03 03 August 2010 Tuesday 82.5


And that's an example of the data within it.

All I want to do is to be able to select certain rows based on month and year, or year

Eg..
Everything from August 2010
Everything from 2010

I'm guessing using WHERE is the way to go and currently I'm going for it using the URL and grabing the variable from there but I have an issue.

Using...
PHP code:
$month = $_GET['month'];
$year = $_GET['year'];

PHP code:
$sql="SELECT * FROM weight WHERE month='$month' & year='$year' OR year='$year'";

Eg :

1) http://server/page.php?year=2010 - Gives everything from 2010

2) http://server/page.php?month=August&year=2010 - Gives everything from 2010 and not August 2010

I can kinda see why it's happening but don't know how to sort it :C
0/0
 Reply   Quote More 

 From:  Drew (X3N0PH0N)  
 To:  ANT_THOMAS      
35356.112 In reply to 35356.111 

You'll need to check whether month is set and if so use the month & year query and if not just query on the year.

 

I don't think it's reasonably doable in a single statement.

 

i.e.

 

if ($month != "") {
$sql="SELECT * FROM weight WHERE month='$month' & year='$year';
} else {
$sql="SELECT * FROM weight WHERE year='$year'";
}

 

(although someone may well have a cleverer solution)


0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  ANT_THOMAS      
35356.113 In reply to 35356.111 
Here's the cleverer solution that doesn't repeat code. :)

php code:
$sql="SELECT * FROM weight WHERE year='$year'";
 
if ($month != "") $sql .= "AND month='$month'";


Some people will probably complain that if statement should be using braces, but meh. Works either way.
0/0
 Reply   Quote More 

 From:  Drew (X3N0PH0N)  
 To:  Peter (BOUGHTONP)     
35356.114 In reply to 35356.113 
Clever lad.

0/0
 Reply   Quote More 

 From:  Matt  
 To:  ANT_THOMAS      
35356.115 In reply to 35356.111 
You're falling foul of operator precedence. The OR in your SQL statement is matching everything from 2010, which undoes the other WHERE clauses.

SQL code:
SELECT * FROM weight WHERE month='August' AND year='2010' OR year='2010'
 


gives you the same result set as:

SQL code:
SELECT * FROM weight WHERE year='2010'


If you ever need to combine OR and AND in your WHERE clause you'll need to bracket them correctly. For example, to get matches for August 2010 and everything in 2010 you would do:

SQL code:
SELECT * FROM weight WHERE (month='August' AND year='2010') OR year='2011'


As for combining your query strings. You'll have to perform tests in PHP and construct the SQL you need based on the presence of the URL query variables

PHP code:
if (isset($_GET['month'], $_GET['year'])) {
 
    $month = mysql_escape_string($month);
    $year = mysql_escape_string($year);
 
    $sql = "SELECT * FROM weight WHERE month = '$month' AND year = '$year'";
 
} else if (isset($_GET['month'])) {
 
    $month = mysql_escape_string($month);
    $sql = "SELECT * FROM weight WHERE month = '$month'";
 
} else if (isset($_GET['year'])) {
 
    $year = mysql_escape_string($year);
    $sql = "SELECT * FROM weight WHERE year = '$year'";
}
 
$result = mysql_query($sql);


Note the use of mysql_escape_string too. It's your best friend.

doohicky

0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  Peter (BOUGHTONP)     
35356.116 In reply to 35356.113 
What Drew said, it works great.

Next question

I'm wanting to use some input boxes to enter the month and/or year to generate the URLs.

The one for just year works fine
HTML code:
<input type="text" id="year" value="Enter Year"/>
<button type="button" onclick="location.href='./date.php?year='+document.getElementById('year').value;">Go To</button></div>


But I want it to grab two variables for the year and month one
HTML code:
<input type="text" id="month" value="Enter Month"/>
<input type="text" id="year1" value="Enter Year"/>
<button type="button" onclick="location.href='./date.php?month='+document.getElementById('month').value'&year='+document.getElementById('year1').value;">Go To</button

Unsurprisingly this messy thing doesn't work.
0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  Matt     
35356.117 In reply to 35356.115 

What's this mysql_escape_string chap and what does it do for me?

 

(As I'm sure you're aware you code works perfectly well also :D )

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Drew (X3N0PH0N)     
35356.118 In reply to 35356.114 
And yet still flawed. :(

I did think it should be doing isset instead of != "", but decided to trust you instead of looking it up.

And in either case I should have mentioned escaping. :'(

Bah.
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  ANT_THOMAS      
35356.119 In reply to 35356.116 
Hmmm, unless I'm being crazy, can you just do this:

html code:
<form action="./date.php" method="get">
	<input type="text" id="year" name="year" value="Enter Year" />
	<button type="submit">Go To</button>
</form>
 
<form action="./date.php" method="get">
	<input type="text" id="month" name="month" value="Enter Month/>
	<input type="text" id="year1" name="year" value="Enter Year" />
	<button type="submit">Go To</button>
</form>


?

(you may also need to restyle the form tags to remove margins/etc this way, but it's a better way than doing onclick=location.href stuff)
0/0
 Reply   Quote More 

 From:  ANT_THOMAS   
 To:  Peter (BOUGHTONP)     
35356.120 In reply to 35356.119 
Indeed you can, much much nicer. Thanks!
0/0
 Reply   Quote More 

Reply to All  
 

1–20  …  61–80  81–100  101–120  121–140  141–158

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats