Solved

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

Posted on 2006-06-10
29
210 Views
Last Modified: 2006-11-18
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
0
Comment
Question by:harpboy918
  • 18
  • 11
29 Comments
 

Author Comment

by:harpboy918
ID: 16877816
I mean to "grad the ID num of the ROW (not database)"
0
 
LVL 5

Expert Comment

by:shaggy_the_sheep
ID: 16877841
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
 

Author Comment

by:harpboy918
ID: 16878127
anyway to pull the acutal row ID from the table - the cronological ID wont equal the ACTUAL ID in the database -
0
 

Author Comment

by:harpboy918
ID: 16878131
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
 

Author Comment

by:harpboy918
ID: 16878132
btw - my ID is a primary key
0
 

Author Comment

by:harpboy918
ID: 16878218
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
 

Author Comment

by:harpboy918
ID: 16878272
let me rephrase - "its" not - I am; i'm not formatting my <tr> and <td>s properly.... aak!
0
 
LVL 5

Expert Comment

by:shaggy_the_sheep
ID: 16878274
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
 
LVL 5

Expert Comment

by:shaggy_the_sheep
ID: 16878282
Okay all you need to do is remove the first <tr>, just under the while statement
0
 

Author Comment

by:harpboy918
ID: 16878327
aak formatting - its skipping a column while outputting text... i know this is relitive simple; but so's my mind
0
 

Author Comment

by:harpboy918
ID: 16878332
wait - never mind I'm working it out - thanks a lot dude!
0
 

Author Comment

by:harpboy918
ID: 16878337
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
 
LVL 5

Expert Comment

by:shaggy_the_sheep
ID: 16878430
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
 

Author Comment

by:harpboy918
ID: 16878438
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 5

Accepted Solution

by:
shaggy_the_sheep earned 220 total points
ID: 16878447
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
 

Author Comment

by:harpboy918
ID: 16878453
$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
 
LVL 5

Expert Comment

by:shaggy_the_sheep
ID: 16878463
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
 

Author Comment

by:harpboy918
ID: 16878498
huh.... that works, but what's with the \ ?

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

thanks man,
0
 
LVL 5

Expert Comment

by:shaggy_the_sheep
ID: 16878511
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
 

Author Comment

by:harpboy918
ID: 16878608
so using that same thought - how would I send two variables to a function popUP using ondblclick="popUP(\'' . $data['id'] . '\')"
thanks
0
 

Author Comment

by:harpboy918
ID: 16878620
         echo '<td style="font-family: verdana; font-size: 8pt" ondblclick="popUP(\'' . $data['id'] . ',' . $_REQUEST[select] .'\')">';   ???
0
 
LVL 5

Expert Comment

by:shaggy_the_sheep
ID: 16878629
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
 

Author Comment

by:harpboy918
ID: 16878636
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
 
LVL 5

Expert Comment

by:shaggy_the_sheep
ID: 16878666
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
 

Author Comment

by:harpboy918
ID: 16878684
lol - copying and pasting that code confuses windows as well :P
0
 
LVL 5

Expert Comment

by:shaggy_the_sheep
ID: 16878693
yea that is definitely a way to screw with your mind! :-)
0
 

Author Comment

by:harpboy918
ID: 16878712
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
 

Author Comment

by:harpboy918
ID: 16878722
might just end up sending it an array...
0
 
LVL 5

Expert Comment

by:shaggy_the_sheep
ID: 16878790
im not too hot on PHP, but should the $_REQUEST[select] be $_REQUEST["select"]??

Richard
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now