A database for my data

From: ANT_THOMAS30 Nov 2008 01:18
To: Peter (BOUGHTONP) 32 of 158

If you want the actual PHP file then I'll C&P the code.

 

Don't shout at me though.

From: Peter (BOUGHTONP)30 Nov 2008 01:26
To: ANT_THOMAS 33 of 158
I'm not sure. :S

You mentioned something about the main content, and also about the full details - my brain can't figure out how you've got things setup.

With jQuery, you can do something along the lines of:
code:
<a href="spectra1.jpg" onclick="$j('img#spectra').attr('src','spectra1.jpg').show(); return false">view spectra</a>


And then under the content you have
code:
<img id="spectra" src="" alt="" style="display:none;"/>


Which isn't perfect but should work.
From: Peter (BOUGHTONP)30 Nov 2008 01:28
To: ANT_THOMAS 34 of 158
quote:
Don't shout at me though.


Is that your way of saying the code is really terrible? :P


If ^that^ stuff doesn't work then you can provide the PHP and I'll give you an exact solution in the morning. Goodnight. :)
EDITED: 30 Nov 2008 01:29 by BOUGHTONP
From: ANT_THOMAS30 Nov 2008 01:33
To: Peter (BOUGHTONP) 35 of 158
That just seems to open the image directly.

http://wakeupbomb.no-ip.org/full.php?id=5

PHP code:
<html>
<head>
<title>NMR Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
 
<?php
mysql_connect("localhost","xxxxxx","******");
mysql_select_db("nmr");
$tableid = $_GET['id'];
$query  = "SELECT id,code,labbookref,contents,solvent,elements,servercode,time,machine,year,month,nmre1,nmrn1,nmre2,nmrn2,nmre3,nmrn3,nmre4,nmrn4,nmre5,nmrn5 FROM nmr WHERE id=$tableid";
$result = mysql_query($query);
 
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "
 
 
		<table border='0' width='auto' style='margin-left: 0px;'>
		<tr>
		<td class='fulltop' width='50px'>{$row['code']}</td>
		<td class='fulltop' width='50px'>{$row['labbookref']}</td>
		<td class='fulltop' width='680px' align='right'>{$row['contents']}</td>
		</tr>
		</table>
 
		<div class='fullmain'>
		{$row['solvent']}<br />
		{$row['elements']}<br />
		{$row['year']}<br />
		{$row['month']}<br />
		<a target='_blank' href='./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.pdf'>{$row['nmre1']}</a><br />
		<a target='_blank' href='./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn2']}/pdata/1/email_{$row['servercode']}_{$row['nmrn2']}_1.pdf'>{$row['nmre2']}</a><br />
		<a target='_blank' href='./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn3']}/pdata/1/email_{$row['servercode']}_{$row['nmrn3']}_1.pdf'>{$row['nmre3']}</a><br />
		<a target='_blank' href='./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn4']}/pdata/1/email_{$row['servercode']}_{$row['nmrn4']}_1.pdf'>{$row['nmre4']}</a><br />
		<a target='_blank' href='./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn5']}/pdata/1/email_{$row['servercode']}_{$row['nmrn5']}_1.pdf'>{$row['nmre5']}</a><br />
		{$row['servercode']}
<br />
<br />
		<a target='image' href='./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png'>{$row['nmre1']}</a><br />
                <a href='./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png' onclick='$j('img#spectra').attr('src','./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png').show(); return false'>{$row['nmre1']} #2</a>
		<img id='spectra' src='' alt='' style='display:none;'/>
                </div>
                <iframe name='image' width='780px' height='550px' frameborder='0' class='fullimage' ></iframe>
 
 
 
 
 
         ";
}
 
 
 
 
?>
</body>
</html>
EDITED: 30 Nov 2008 01:36 by ANT_THOMAS
From: Peter (BOUGHTONP)30 Nov 2008 14:27
To: ANT_THOMAS 36 of 158
Ah, I forgot to explicitly mention the bit where you need to add jQuery library to the page.

Download this file, put it in the same dir as the stylesheet and then insert this code just before the </head>
code:
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript">$j = jQuery.noConflict();</script>




And yes, that code is terrible. :P

You could at least save yourself some headache by doing:

PHP code:
$prefix = "./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/";


Just before the echo, after the opening brace.

And then replacing the start of all your links with {$prefix} to make things a bit more readable.
EDITED: 30 Nov 2008 14:31 by BOUGHTONP
From: ANT_THOMAS30 Nov 2008 14:33
To: Peter (BOUGHTONP) 37 of 158
Thank you, and good idea. I'll give that a go.
From: ANT_THOMAS30 Nov 2008 14:50
To: Peter (BOUGHTONP) 38 of 158
I've stuck these lines in...

PHP code:
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript">$j = jQuery.noConflict();</script>

(between the head tags)

then

PHP code:
<a href='./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png' onclick='$j('img#spectra').attr('src','./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png').show(); return false'>{$row['nmre1']} #2</a>
<br />
<img id='spectra' src='' alt='' style='display:none;'/>


But it just opens the image directly instead of within the page.
EDITED: 30 Nov 2008 14:51 by ANT_THOMAS
From: Ally30 Nov 2008 17:20
To: ANT_THOMAS 39 of 158
Two possibilities:

  • The js is erroring out before it gets to return false- therefore it'll just go ahead and follow the link. It might then also wipe the JS error console and you won't get to see what the problem is. Try just doing a href to # to test it.
  • return false isn't actually a terribly reliable way of stopping things from happening. I forget the details now but in MooTools you'd call e.stop() to stop all event bubbling. But Peter made you use jQuery, so he can bloody well sort out his own mess, can't he?
From: Peter (BOUGHTONP)30 Nov 2008 18:02
To: ANT_THOMAS 40 of 158
Firstly, make sure you're testing in Firefox, and have Firebug installed, as it makes debugging much easier.

Then, you can either:
1) switch the href to # like Ally says
2) rename it to xhref which is a little simpler

And then look in Firebug's console to see what it says about the error.
From: ANT_THOMAS30 Nov 2008 20:17
To: Peter (BOUGHTONP) 41 of 158
Changed it to <x href and firebug says

syntax error
http://wakeupbomb.no-ip.org/full.php?id=5
Line 1
(

When it is an 'a' the code outputted by the page is:

PHP code:
<a false="" return="" ).show();="" email_2008-10-09-ab-49_10_1.png="" 1="" pdata="" 10="" 2008-10-09-ab-49="" nmr="" ab="" data="" oct="" 2008="" bruk400data="" .="" ,="" src="" ).attr(="" img#spectra="" onclick="(" href="./nmr/bruk400data/2008/Oct/data/AB/nmr/2008-10-09-AB-49/10/pdata/1/email_2008-10-09-AB-49_10_1.png">F #2</a>
<br/>
<img id="spectra" style="display: none;" alt="" src=""/>


I see that somehow there is a cheeky extra RHS bracket there:

<a false="" return="" ).show();=""

The code I on the page is:

PHP code:
<a href='./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png' onclick='$j('img#spectra').attr('src','./nmr/{$row['machine']}/{$row['year']}/{$row['month']}/data/AB/nmr/{$row['servercode']}/{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png').show(); return false'>{$row['nmre1']} #2</a>
<br />
<img id='spectra' src='' alt='' style='display:none;'/>


I should really start using your $prefix idea because the long directory structures are doing my head in now.
From: Peter (BOUGHTONP)30 Nov 2008 20:30
To: ANT_THOMAS 42 of 158
Eh? I didn't mean <x href=""... - I meant <a xhref=""...

Anyway, no matter - the problem is that you've got single quotes for the HTML attributes, which is conflicting with the JavaScript single quotes, and causing all that funniness to occur.

Use double quotes for the onclick and it should be fine - but that means escaping them with a backslash so PHP echo doesn't get all confused:

code:
onclick=\"...\"


Once you've got that all working, you can replace the URL within onclick section with this.href so you don't have to repeat yourself but also keep your code accessible.
From: ANT_THOMAS30 Nov 2008 21:34
To: Peter (BOUGHTONP) 43 of 158
Done, no worky :((

It now, as you would image imagine, looks like this with the backslashes, two needed I assume?

PHP code:
<a href='$prefix{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png' onclick=\"$j('img#spectra').attr('src','$prefix{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png').show(); return false\">{$row['nmre1']} #2</a>
<br />
<img id='spectra' src='' alt='' style='display:none;'/>
 
</span


Still opens the image directly and not within the page.
EDITED: 30 Nov 2008 21:46 by ANT_THOMAS
Message 35356.44 was deleted
From: Peter (BOUGHTONP) 1 Dec 2008 00:13
To: ANT_THOMAS 45 of 158
Ah, bloody PHP - you also need to escape the $ of $j with \

So, your first line now becomes:
PHP code:
<a href='$prefix{$row['nmrn1']}/pdata/1/email_{$row['servercode']}_{$row['nmrn1']}_1.png' onclick=\"\$j('img#spectra').attr('src', this.href ).show(); return false\">{$row['nmre1']} #2</a>

(That also has the second url replaced with this.href)

Once you've done that it'll work.
EDITED: 1 Dec 2008 00:14 by BOUGHTONP
From: ANT_THOMAS 1 Dec 2008 00:46
To: Peter (BOUGHTONP) 46 of 158
Yay, thank you for your persistence Peter!
From: ANT_THOMAS 4 Dec 2008 00:31
To: ALL47 of 158
Right. It is all working very nicely now. I had missed the option in IrfanView for PDF to PNG batch conversions so that's all good except for the image quality being a bit poor, but nevermind.

I have a question tho. I want to pull, for example, 5 consecutive rows from my database. Sounds simple enough to me, but I can't get it working how I want.

Current code is...

PHP code:
$tableid = $_GET['id'];
$query  = "SELECT * FROM nmr LIMIT 5 OFFSET $tableid";
 


If id=5 then it gives me entry 5-9, as you would expect. Problem being, the first id in my database isn't 1 due to deleting a few, and if I was to delete entries at a later date even if the first one was 1 it would mess things up. So clearly this code is wrong and I realise this.

I can already get id from the address bar but I then want it to pull the record that has that id and the next 4 records. So the limit is 5 including the record with the id in the address bar.

I've searched and searched but had no luck :(
From: andy 4 Dec 2008 00:33
To: ANT_THOMAS 48 of 158

select * from nmr where id >= offset limit 5

 

edit: oh and you prob want a "order by id asc" just to make sure.

EDITED: 4 Dec 2008 00:34 by ANDY
From: Matt 4 Dec 2008 00:42
To: ANT_THOMAS 49 of 158
php code:
$tableid = $_GET['id'];
$query  = "SELECT * FROM nmr WHERE id >= $tableid LIMIT 5";
 


Of course change the name of the id column, if it's different.

Also, if you're not already you should really be using mysql_escape_string / mysql_real_escape_string on the variables you take from $_GET / $_POST / $_COOKIE etc. to use in your queries, or you risk SQL injection.

I could for instance do:

http://www.yourdomain.com/script.php?id=;DELETE%20*%20FROM%20nmr

And empty your database for you.
From: ANT_THOMAS 4 Dec 2008 01:00
To: Matt 50 of 158
Thank you both of you.

Hmm, I think I should too because someone emptying my database would make me cry.

Would this be sufficient?

PHP code:
 
$getid = $_GET['id'];
$tableid = mysql_escape_string($getid);
$query  = "SELECT * FROM nmr WHERE id >= $tableid LIMIT 5";
 
From: Peter (BOUGHTONP) 4 Dec 2008 01:08
To: ANT_THOMAS 51 of 158
Why use two steps?

Simpler to just do this:
code:
$tableid = mysql_escape_string($_GET['id']);

?