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
harpboy918Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
shaggy_the_sheepConnect With a Mentor Commented:
lol

how many rows do actually want to have..? you have 2 opening tags, but only 1 closing. I'm gonna guess that you want 2.

$results = mysql_query($newq);
$num = 1;
while ($data = mysql_fetch_array($results)) {
     echo "<tr onclick='trClickDOM(this);'>";
     for ($z=0; $z < $i; $z+=1)
     {
          echo '<td style="font-family: Arail; font-size: 9pt">';
          echo 'Text In Field One</td>';
          echo '<td style="font-family: Arail; font-size: 9pt" ondblclick="alert(this.num)">';
          echo $data[$field_names[$z]] . '</td>';
     }    
     $num +=1;
    echo "</tr>";
}
?>

Just a quick question....when you dblclick your field, do you actually get a value in the alert of just an empty box??

Richard
0
 
harpboy918Author Commented:
I mean to "grad the ID num of the ROW (not database)"
0
 
shaggy_the_sheepCommented:
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
harpboy918Author Commented:
anyway to pull the acutal row ID from the table - the cronological ID wont equal the ACTUAL ID in the database -
0
 
harpboy918Author Commented:
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
0
 
harpboy918Author Commented:
btw - my ID is a primary key
0
 
harpboy918Author Commented:
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>";
}
?>
0
 
harpboy918Author Commented:
let me rephrase - "its" not - I am; i'm not formatting my <tr> and <td>s properly.... aak!
0
 
shaggy_the_sheepCommented:
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
0
 
shaggy_the_sheepCommented:
Okay all you need to do is remove the first <tr>, just under the while statement
0
 
harpboy918Author Commented:
aak formatting - its skipping a column while outputting text... i know this is relitive simple; but so's my mind
0
 
harpboy918Author Commented:
wait - never mind I'm working it out - thanks a lot dude!
0
 
harpboy918Author Commented:
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?
0
 
shaggy_the_sheepCommented:
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
0
 
harpboy918Author Commented:
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>";
}
?>
0
 
harpboy918Author Commented:
$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!
0
 
shaggy_the_sheepCommented:
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
0
 
harpboy918Author Commented:
huh.... that works, but what's with the \ ?

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

thanks man,
0
 
shaggy_the_sheepCommented:
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
0
 
harpboy918Author Commented:
so using that same thought - how would I send two variables to a function popUP using ondblclick="popUP(\'' . $data['id'] . '\')"
thanks
0
 
harpboy918Author Commented:
         echo '<td style="font-family: verdana; font-size: 8pt" ondblclick="popUP(\'' . $data['id'] . ',' . $_REQUEST[select] .'\')">';   ???
0
 
shaggy_the_sheepCommented:
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
0
 
harpboy918Author Commented:
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
0
 
shaggy_the_sheepCommented:
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
0
 
harpboy918Author Commented:
lol - copying and pasting that code confuses windows as well :P
0
 
shaggy_the_sheepCommented:
yea that is definitely a way to screw with your mind! :-)
0
 
harpboy918Author Commented:
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?
0
 
harpboy918Author Commented:
might just end up sending it an array...
0
 
shaggy_the_sheepCommented:
im not too hot on PHP, but should the $_REQUEST[select] be $_REQUEST["select"]??

Richard
0
All Courses

From novice to tech pro — start learning today.