Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamic table problem...

    I have a problem.

    The MySQL database has over 400 rows and 23 colums.

    The user can view all 400+ rows if desired. No prob with that.

    The user can choose which of the 23 colums of data to view,
    from a minimum of 3 columns to the full 23.

    I'm using checkboxes to determine which columns the user
    wants to view in the results table.

    I can't figure out how to dynamically create the table from
    user input...

    Any help would be greatly appreciated.

    Thanks...
    Oorik

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well the checkboxes are going to be in the table header I assume? This will have to be a form, and there will have to be some sore of submit button which will post the form variables back to this page (or to some other page...?).

    Based on whichever checkboxes come through (if you didn't know, if a checkbox is not checked, it will not be posted at all), you'd know which columns to show. You'd have to build the proper query for that and store the result in $result.

    Then to create the table, you'd do something like this:
    PHP Code:
    <table>
     <tbody>
      <?php
       
    while($row mysql_fetch_array($result)) {
      
    ?>
       <tr>
        <?php
         
    foreach($row as $columnName=>$columnValue) {
        
    ?>
         <td>
          <?php echo $columnValue?>
         </td>
        <?php
         
    }//foreach
        
    ?>
       </tr>
      <?php
       
    }//while
      
    ?>
     </tbody>
    </table>
    If that doesn't make sense, or doesn't work -- sorry! Hopefully that will point you in the right direction though..

    Hope that helps,
    Sadiq.

  • #3
    New Coder
    Join Date
    Mar 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    All of your assumptions are correct.

    I'm new at PHP, obviously, and I knew
    I'd have to build the query from the
    checkboxes. (I'm still figuring that out
    too, lol)

    I just couldn't seem to get a mental
    picture of what to do and there didn't
    seem to be any questions like mine in
    any of the forums I searched.

    Your code helps a great deal.

    I'll post again when I figure out how to
    build the query. It shouldn't be too hard
    using a foreach statement.

    Thanks, Sadiq
    Oorik

  • #4
    New Coder
    Join Date
    Mar 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    This works... based on Sadiq's suggestion. BUT...

    PHP Code:

    do {
       echo 
    "<tr>";
       
    $colcount 1
        foreach(
    $result as $value) {
            if (
    $colcount == 1) {
              echo 
    "<td bgcolor='#333366' align=left><font size=1 color=lightyellow face='arial narrow'>" " &nbsp; " $myrow[$value] . "</font></td>"; }
            elseif (
    $myrow[$value] == '0.000') { 
                echo 
    "<td bgcolor='#333366'></td>"; }
                else {        
              echo 
    "<td bgcolor='#333366' align=center><font size=1 color=lightyellow face='arial narrow'>" $myrow[$value] . "</font></td>"; }
        
    $colcount $colcount 1;
        }
       echo 
    "</tr>";
       } 
    while (
    $myrow mysql_fetch_array($answer));
    echo 
    "</tr></table>\n"
    Now, of course, I have another problem...

    If the user doesn't click a checkbox it still runs
    but delivers a warning. I'd rather validate first,
    immediately after 'submit' is clicked but can't
    seem to get any validation to work.

    I'd like to post a message to 'click something'
    and stop processing the script , just return
    them to the form page.

    Any suggestions?
    Last edited by oorik; 03-22-2004 at 01:39 PM.
    Oorik

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Javascript form validation perhaps?

    I wonder if this will help:
    http://www.codeave.com/javascript/code.asp?u_log=7062

    Is your application online? If you give us a link, perhaps we can see what you're actually talking about and help you carve a solution.

    Good luck,
    Sadiq.

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi, oorik. Please could you use PHP tags when posting code, you can read about that in a tutorial I wrote: <http://www.freeipb.net/phptags>

    Anyway, to your problem, try something like this:

    Code:
    <p>
       <input type="checkbox" name="selections[]" value="column_name />View Column Name<br />
       <input type="checkbox" name="selections[]" value="colum_name2" />View Column Name 2
    </p>
    (you would need to obviously add more checkboxes, but notice how the name is exactly the same for each one, "selections[]".

    Then for your PHP code:

    PHP Code:
    <?php

    if(count($_POST['selections']) < 3){

       echo 
    "You need to enter more fields to view";

    } else {

       
    $slfrom "";

       foreach(
    $_POST['selections'] as $fieldname){

          
    $slfrom .= $fieldname.", ";

       }

       
    $slfrom substr_replace($slfrom"", -2);

       
    $query "SELECT FROM ".$slfrom." WHERE blah...."// insert actual end of query

       
    $printheader 1// tells it whether to do a header or not

       
    if($data mysql_query($query)){

          while(
    $row mysql_fetch_assoc($data)){
             
             foreach(
    $row as $columName => $columnValue){
                
                
    // print data
                
             
    }

          }

       } else {

          echo 
    "mysql query failed";

       }

    }
    Hope this helps you a bit... sorry for leaving it a bit incomplete but ive got to go to school

  • #7
    New Coder
    Join Date
    Mar 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I considered javascript but don't quite know how it works.

    For example, does Java validate when the submit button
    is clicked? If so, there'll be 2 things happening upon a
    submit... The javascript will run AND the results from the
    form will be sent to php. Both will happen simultaneously.

    I can't wrap my mind around how it would validate before
    sending the form data.

    I thought about simply suppressing the warnings... That just
    hides the problem, I'd rather fix it.

    I'll try the code from missing-score. Just the first line gives me
    an idea. However, if the count is 0 i'd need a notice to the user
    with an 'OK' button that redirects to the form page. Can php do
    that? If the count is anything but 0, it's good to go.

    Here's the link.

    Thanks people.
    Oorik

  • #8
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There's an event recognized by Javascript called onSubmit. You would assign a function to be called onSubmit.

    Now what happens is that when the form is submitted, this function is called. If this function returns true (meaning it's okay to submit), the form will be submitted. If this function returns false, then the form will not submit, leaving you on the current page.

    This is shown in that link I left in my previous post.

    Hope that helps,
    Sadiq.

  • #9
    New Coder
    Join Date
    Mar 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript didn't work...

    I may have done something wrong but I don't
    think so... Probably missing something simple.

    It's not executing any of the javascript. At least
    it doesn't appear to be executing it.

    Go to the link I gave you above then 'view/source'.

    The submit button still sends to php.

    Hmmm...

    I think this is the only javascript I've ever used
    that didn't work right away, lol.

    Prob just me...

    Please look and tell me what I've overlooked...

    Thanks again, this time for your patience.
    Oorik

  • #10
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry it took me a bit to reply.. stupid IE wouldn't show the source because I had 'too many' temporary internet files...

    Anyways, looks like you're missing some closing braces in your Javascript:
    Code:
    function checkbox_checker() {
    
     // set var checkbox_choices to zero
    
     var checkbox_choices = 0;
    
     // Loop from zero to the one minus the number of checkbox button selections
     for (counter = 0; counter < classsearch.checkbox.length; counter++) {
    
      // If a checkbox has been selected it will return true
      // (If not it will return false)
      if (classsearch.checkbox[counter].checked) { 
       checkbox_choices = checkbox_choices + 1; 
      }
     }
     
     if (checkbox_choices == 0 ) {
      // If there were no selections made display an alert box 
      msg="You must select at least one checkbox.\n\n"
      msg=msg + "Please try again."
      alert(msg)
      return (false);
     }
     else {
      // If there were one or more selections made return = true 
      // alert("Please make three selections. \n" + checkbox_choices + " entered so far.")
      return (true);
     }
    }
    I made a few other changes, namely:
    -changed = to ==. = is an assignment, whereas == checks for equality: a = b: a stores the value b stored. a == b: is a storing the same value as b? true or false....
    -combined your two if statements into one if/else statement. this is because of the return statements and the fact that checkbox_choices will either be 0 or greater, so if is 0 else is greater.

    Hope that makes sense. I didn't inspect your code much more than that, but I expect that will at least get your code running.

    If you're using IE, then you can check your Javascript console (bottom left corner of your browser. if there is a yellow triangle, there's a problem and it'll tell u the line number and some lame message..)

    EDIT: BTW, in the future, when coding Javascript, or PHP for that matter, you should try to indent your code as much as possible, that way you know what braces line up with what and which blocks of code belong to what, etc... just a thought (after viewing the source of your webpage..)

    Hope that helps,
    Sadiq.
    Last edited by sad69; 03-23-2004 at 05:54 PM.

  • #11
    New Coder
    Join Date
    Mar 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I copy/pasted your altered code over my old code,
    (which was copy/pasted from the site you entered
    above, with all the mistakes), I simply changed a
    few numbers.

    It still doesn't work.

    The error message says:

    "classsearch.checkbox.length" is null or doesn't exist.

    for (counter = 0; counter < classsearch.checkbox.length; counter++)

    So I temporarily deleted the '.length' part... The script
    rane fine, with the small exception that the alert was
    shown whether any checkbox was clicked or not. I put
    the '.length' portion back.

    Is '.length' what we want there? I don't know enough
    about javascript to determine whether or not that piece
    of code is good.

    The guy who wrote that code used = for ==, maybe he
    used the wrong thing there too...?!?
    Oorik

  • #12
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Umm...

    Ok the problem is that you've given all the checkboxes the same name. At this point, since the data has not yet been submitted to the server, the names are merely strings. Since you've given all the checkboxes the same name, you cannot refer to them individual by their name.

    I know it looks like you've created an array, but that array will not 'exist' (for lack of a better word) until it is received by the server.

    If you can, I would give each checkbox a unique id, and use a loooong if statement to test if any of the checkboxes have been checked.

    Another way, would be to use DOM to 'loop' through your checkboxes.

    The DOM method actually sounds good. Try the following:

    Replace:
    Code:
    <tr>
    			<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>General</font></td>			
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="slot"><font face='arial' size=-1 color=lightyellow>&nbsp;Slot  &nbsp;  &nbsp;</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="set1"><font face='arial' size=-1 color=lightyellow>&nbsp;Set1</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="set2"><font face='arial' size=-1 color=lightyellow>&nbsp;Set2 &nbsp;</font></td>
    
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="set3"><font face='arial' size=-1 color=lightyellow>&nbsp;Set3</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="con"><font face='arial' size=-1 color=lightyellow>&nbsp;Con  &nbsp;  </font></td>
    		</tr>
    		<tr>
    			<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>Tactics</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="g_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Ground</font></td>
    
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="t_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Thief</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="m_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Magic</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="a_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Air  &nbsp;</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="s_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Scout</font></td>
    		</tr>
    		<tr>
    			<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>Attack</font></td>
    
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="g_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Ground</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="t_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Thief</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="m_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Magic</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="a_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Air  &nbsp;</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="s_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Scout</font></td>	
    		</tr>
    		<tr>
    
    			<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>Defend</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="g_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Ground</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="t_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Thief</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="m_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Magic</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="a_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Air  &nbsp;</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="s_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Scout</font></td>	
    		</tr>
    with:
    Code:
    <tbody id="tb_checkboxes">
    <tr>
    			<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>General</font></td>			
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="slot"><font face='arial' size=-1 color=lightyellow>&nbsp;Slot  &nbsp;  &nbsp;</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="set1"><font face='arial' size=-1 color=lightyellow>&nbsp;Set1</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="set2"><font face='arial' size=-1 color=lightyellow>&nbsp;Set2 &nbsp;</font></td>
    
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="set3"><font face='arial' size=-1 color=lightyellow>&nbsp;Set3</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="con"><font face='arial' size=-1 color=lightyellow>&nbsp;Con  &nbsp;  </font></td>
    		</tr>
    		<tr>
    			<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>Tactics</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="g_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Ground</font></td>
    
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="t_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Thief</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="m_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Magic</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="a_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Air  &nbsp;</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="s_tactic"><font face='arial' size=-1 color=lightyellow>&nbsp;Scout</font></td>
    		</tr>
    		<tr>
    			<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>Attack</font></td>
    
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="g_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Ground</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="t_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Thief</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="m_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Magic</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="a_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Air  &nbsp;</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="s_att"><font face='arial' size=-1 color=lightyellow>&nbsp;Scout</font></td>	
    		</tr>
    		<tr>
    
    			<td bgcolor="#333366" align=center><font face='arial' size=-1 color=lightgreen>Defend</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="g_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Ground</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="t_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Thief</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="m_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Magic</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="a_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Air  &nbsp;</font></td>
    			<td bgcolor="#333366" align=center><input type="checkbox" name="mycheckbox[]" value="s_def"><font face='arial' size=-1 color=lightyellow>&nbsp;Scout</font></td>	
    		</tr>
    </tbody>
    Then use:
    Code:
    function checkbox_checker() {
     var checkbox_choices = 0;
    
     var tb = document.getElementById("tb_checkboxes");
     for(var i = 0; i < tb.childNodes.length; i++) {
      var tr = tb.childNodes.item(i);
      for(var j = 1; j < tr.childNodes.length; j++) {
       if(tr.childNodes.item(j).firstChild.checked) {
        checkbox_choices++;
       }
      }
     }
    
     if(checkbox_choices == 0) {
      // If there were no selections made display an alert box 
      msg="You must select at least one checkbox.\n\n"
      msg=msg + "Please try again."
      alert(msg)
      return (false)
     }
     else {
      // If there were one or more selections made return = true 
      // alert("Please make three selections. \n" + checkbox_choices + " entered so far.")
      return (true);
     }
    }
    Again I haven't tested this code because I'm just falling asleep... hopefully that works out for you. Post back any error messages, and I'll help you debug it.

    Nite,
    Sadiq.

  • #13
    New Coder
    Join Date
    Mar 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Excellent !!!

    It worked !!

    I added two tags:

    <tbody id="tb_checkboxes">

    and

    </tbody>

    Then changed the check function to what you
    gave me... Excellent work, Sidiq.

    I wish there was a way to do this strictly using
    php... It seems that having to learn 3 different
    languages to accomplish one task is a bit much.

    Now to figure out how to use 'style' to reduce the
    amount of code... And learn Java... lolol.

    I can't honestly tell you how much I appreciate
    your assistance in this little 'learning experience'.

    Thank you...
    Oorik

  • #14
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey no problem!

    What you will find is that PHP is only limited to server-side processing. So unless you want to send information from the client to the server to process it and then back, everytime, then it's a good idea to use client-side scripting, which Javascript provides.

    If you were to use purely PHP, then it seem that the client's browser kept refreshing which can be annoying..

    Of course now you run into the problem where some of your visitors may have Javascript disabled or are running some weird browser that doesn't have a proper version of Javascript installed or something. But these days it isn't too common...

    It's hard to win everytime, but sometimes it doesn't hurt to have a few tools in your belt to solve various problems.

    Anyway, good luck!
    Sadiq.

  • #15
    New Coder
    Join Date
    Mar 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Textboxes...?!?

    Ok....

    Checkbox validation works wonderfully...

    For 3 days I haven't been able to alter
    the validation routine to work with text-
    boxes...

    Searching google and other places shows
    me how to validate a single textbox...

    I have 15 on a single form (this is all for
    the same site as the problem in my other
    posts...).

    Of these textboxes, again, only 1 need
    be filled to pass to php and only the
    numbers '0 - 9' and the decimal point '.'
    are legal.

    Is there a way (Onblur) to validate on the
    fly and relocate the cursor to the incorrect
    textbox (focus), before the submit button
    is clicked?

    As you can see, I have done a little digging
    but your code is so short and 'to the point',
    I was hoping I could simply adapt it. Alas,
    it's hard and time consuming searching the
    web, although I tried...

    Any help would be greatly appreciated...
    (again), lol.
    Oorik


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •