Link to home
Start Free TrialLog in
Avatar of harpboy918
harpboy918

asked on

PHP/MYSQL database outputte as a table... need to click on and get the value in a cell.

hopefully i'm explaining well in the subject, but I have a database that's outputting data into tables and I was wondering it I couldd use "<TR ONCLICK" or something to grab the data from the row(i just want to grab the ID num of the database by clicking on the table) I kknow I'm going to have to use javascript, but I 'm not certian of the code - any help guys?

you guys rock! mark
Avatar of harpboy918
harpboy918

ASKER

I mean to "grad the ID num of the ROW (not database)"
Hi

What you could do is as you are righting out the table using php change what you have to include the id.

Lets say you are writing your table something like this:
$row = '<tr><td>' . $data . '</td></tr>'
Simply change it to this
$id is an id that gets incremented with each loop of the database
$row = '<tr onclick="alert(this.id)" id="' . $Id . '"><td>' . $data . '</td></tr>'

Let me know if that is what you want

Richard
anyway to pull the acutal row ID from the table - the cronological ID wont equal the ACTUAL ID in the database -
or, better yet - using mysql, i've set up my ID variable to auto increment, but if I delete a certain record, the numbers wont reassign to reflect that IE

1
2
3
4  Test DELETE ME
5

1
2
3
5
6

etc.. thanks
btw - my ID is a primary key
also - it's sort of scewing my table structure with the  javascript -

$results = mysql_query($newq);
$num = 1;
while ($data = mysql_fetch_array($results)) {
      echo "<tr>";
      echo "<TR ONCLICK='trClickDOM(this);'>";
//      echo "<tr onmouseover='ToggleRowColor(this, true)' onmouseout='ToggleRowColor(this, false)'>";
      for ($z=0; $z < $i; $z+=1)
      {
            echo "<td><font face= 'verdana' size = 0>";
          echo "<td onclick = 'alert(this.id);'>";
            echo "<font face = 'verdana' size = 0>";
            echo $data[$field_names[$z]];
             echo "</font></td>";
      }
      $num +=1;
  echo "</tr>";
}
?>
let me rephrase - "its" not - I am; i'm not formatting my <tr> and <td>s properly.... aak!
Okay, rather than using an auto incrementing id, could you not just write the primary key index into the row tag, eg:
'<tr id="' . $PRIMARYKEYFIELD . ' onclick="trClickDOM(this);">';

That would maintain the relation to your database

Richard
Okay all you need to do is remove the first <tr>, just under the while statement
aak formatting - its skipping a column while outputting text... i know this is relitive simple; but so's my mind
wait - never mind I'm working it out - thanks a lot dude!
But - here's another one for (increase the anty) - instead of just an alert , how could i open a new (size-scaled) window, while passing that index number to that newly opened  window?
im guessing your 'trClickDOM() function is something like alert(blahblah)?

just change that function to this
function 'trClickDOM(id){

window.open('MYPAGE.php?id=' + id,'ShowId','width=400,height=400,top=200,left=200','ShowId');

}

That should do it for ya, let me know if you need anything else

Richard
can you change font preferences within a <td>?  I know this sounds stupid, but I got everthing aligned - but I cant switch the font size down.  dont gag when you see my code..

$results = mysql_query($newq);
$num = 1;
while ($data = mysql_fetch_array($results)) {
      echo "<TR ONCLICK='trClickDOM(this);'>";
//      echo "<font face = 'verdana' size = 0>";
      for ($z=0; $z < $i; $z+=1)
      {
            echo "<font face = 'arial' size = 1>";
            echo "<td font face = 'arial' size = 1>";
            echo "<td font face = 'arial' size = 1 ondblclick =alert(this.num)>";
            echo $data[$field_names[$z]];
            echo "</td>";
      }      
      $num +=1;
    echo "</tr>";
}
?>
ASKER CERTIFIED SOLUTION
Avatar of shaggy_the_sheep
shaggy_the_sheep

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
$results = mysql_query($newq);
$num = 1;
while ($data = mysql_fetch_array($results)) {
      echo "<TR ONCLICK='trClickDOM(this);'>";
//      echo "<font face = 'verdana' size = 0>";
      for ($z=0; $z < $i; $z+=1)
      {
            echo "<td font face = 'arial' size = 1 ondblclick =alert(this.num)>";
            echo $data[$field_names[$z]];
            echo "</td>";
      }      
      $num +=1;
    echo "</tr>";
}
?>

only "undefined" returns - that's fine, I just wanted to get the premise working, and I'll sub in $data['id'] and sent that to the popup; then query the ID and project done..!

yeah thoes open tags were just me being a dumbass...... thanks so far!
no probs, okay that seems okay. Just remember when you sub in the PHP script you need to enclose it within single quotes, so it would be:

ondblclick="alert(\'' . $data['id'] . '\')"

i think that is how you 'escape' characters in PHP, not entriely sure though!!

Richard
huh.... that works, but what's with the \ ?

just when I thought I was sort  of getting a grip on syntax....

thanks man,
because the output of the PHP echo is all within single quotes, like:
echo 'Hello World!'
if you used plain single quotes then the PHP parser would interpret that as the end of the string. But placing a backslash infront of the single quote it converts it to a literal single quote, ensuring the PHP parser actually writes a ' rather than ending the string.

I hope that makes sense

Richard
so using that same thought - how would I send two variables to a function popUP using ondblclick="popUP(\'' . $data['id'] . '\')"
thanks
         echo '<td style="font-family: verdana; font-size: 8pt" ondblclick="popUP(\'' . $data['id'] . ',' . $_REQUEST[select] .'\')">';   ???
not quite, you need to enclose each variable within single quotes (escaped ones in this case)

so you line was:
echo '<td style="font-family: verdana; font-size: 8pt" ondblclick="popUP(\'' . $data['id'] . ',' . $_REQUEST[select] .'\')">';

but it needs to be:
echo '<td style="font-family: verdana; font-size: 8pt" ondblclick="popUP(\'' . $data['id'] . '\',\'' . $_REQUEST[select] .'\')">';

Richard
wow - good  lord - I did read your explanation about PHP string interpretations, it just probably would have taken me hella long to do the above .. thanks again
no probs.

Just a note: i find it easier to write the javascript normally, and then convert it to PHP (well ASP in my case). Means your less likely to make small syntax errors.

Well hope you're all sorted now!

Richard
lol - copying and pasting that code confuses windows as well :P
yea that is definitely a way to screw with your mind! :-)
echo '<td style="font-family: verdana; font-size: 8pt" ondblclick="popUP(\" . $data['id'] . '\',\" . $_REQUEST[select] .'\')">';

aak - getting parser error.... typed it in character for character... u still up for it?
might just end up sending it an array...
im not too hot on PHP, but should the $_REQUEST[select] be $_REQUEST["select"]??

Richard