dcsimg
www.webdeveloper.com
Results 1 to 13 of 13

Thread: Table variable to new web page

  1. #1
    Join Date
    Oct 2014
    Posts
    8

    Table variable to new web page

    I have a table populated from a DB and the values within the table, when displayed are a hyperlink to a new page, which is the same page for everything. The point of the new page is change the value in the CLICKED cell. The goal is to change the value of the chosen cell to another predetermined value. I am unsure how to get the particular value from the CLICKED cell to the new page??

    Here is the code I have now:

    $result = mysqli_query($con,"SELECT * FROM volunteer");

    echo "<table class='std'>
    <tr>
    <th>CLASS</th>
    <th>HEAD FLAGGER</th>
    <th>CORNER FLAGGER 1</th>
    <th>CORNER FLAGGER 2</th>
    <th>CENTER SCORER 1</th>
    <th>CENTER SCORER 2</th>
    <th>BOARD 1</th>
    <th>BOARD 2</th>
    <th>STAGER</th>
    </tr>";

    while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    $website = "localhost/vchange.php";
    $class = $row['class'];
    $head_flagger = $row['head_flagger'];
    $corner_one = $row['corner_one'];
    $corner_two = $row['corner_two'];
    $center_one = $row['center_one'];
    $center_two = $row['center_two'];
    $board_one = $row['board_one'];
    $board_two = $row['board_two'];
    $stager= $row['stager'];

    echo("\t<tr>\n");
    echo("\t\t<td>" . "$class" . "</td>\n");
    echo("\t\t<td>" . "<a href='http://$website' target = '_blank'>" . "$head_flagger" . "</a></td>\n");
    echo("\t\t<td>" . "<a href='http://$website' target = '_blank'>" . "$corner_one" . "</a></td>\n"); echo("\t\t<td>" . "<a href='http://$website' target = '_blank'>" . "$corner_two" . "</a></td>\n");
    echo("\t\t<td>" . "<a href='http://$website' target = '_blank'>" . "$center_one" . "</a></td>\n"); echo("\t\t<td>" . "<a href='http://$website' target = '_blank'>" . "$center_two" . "</a></td>\n");
    echo("\t\t<td>" . "<a href='http://$website' target = '_blank'>" . "$board_one" . "</a></td>\n");
    echo("\t\t<td>" . "<a href='http://$website' target = '_blank'>" . "$board_two" . "</a></td>\n");
    echo("\t\t<td>" . "<a href='http://$website' target = '_blank'>" . "$stager" . "</a></td>\n"); echo "</tr>";
    }
    mysqli_close($con);
    ?>
    </table>

    Initially to start, all the values are 'XX' and the user clicks the HREF and a new page (vchange.php) will open and change the 'XX' to a value already determined.

    I am not sure how to know the location of that chosen cell to the new webpage!!! STUCK!

    I would like to remain in PHP as that is what I am learning.

    THANKS!

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,094
    Add a GET parm to your anchor tag which is unique for each anchor. Then in the called script just grab that GET parm and use it.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  3. #3
    Join Date
    Oct 2014
    Posts
    8
    Thanks, I have been playing with that, but I am stuck on how to GET the right table cell....can you give me a bit of the code to understand?

  4. #4
    Join Date
    Oct 2014
    Posts
    8
    Here is what I been working on, but I getting errors. Something is not right here...

    echo("\t\t<td>" . "<a href='http://$website?V_ID=$row['vol_job']' target = '_blank'>" . "$head_flagger" . "</a></td>\n");

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,094
    When that anchor triggers whatever script it is going to, you will have a $_GET['V_ID'] to give you some idea of exactly which cell was clicked. Unless $row['vol_job'] is not unique across the whole table. What are you trying to retrieve with this info - the exact row of the table clicked on? If so you should use the record key of the dat in that row.

    Why do you need an anchor tag in every cell of the rows? They look like they are all going to the same place so why repeat them?

    Plus - you have an extra tr tag being output for each row.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  6. #6
    Join Date
    Oct 2014
    Posts
    8
    Thanks, sorry for the delay getting back on this, been sick and working too much at my real job!

    V_ID is a row identifier.

    There is an Anchor Tag in each cell as the user picks which cell they want to change. The cells are populated with 'XX' to start and then are to change to there number as they choose them. each user will change up to 5 cells as they volunteer for different jobs.

    I will work on your suggestions, just stuck on how to move forward.

    Thanks.

  7. #7
    1) Where's your SCOPE, THEAD and TBODY

    2) why are you using multiple echo to do one echo's job

    3) why are you using double-quotes and parsed tabs, just trying to make it harder to read?

    4) why are you wasting time making copies of values for nothing more than wasting memory?

    5) why are you doing string additions BETWEEN static strings?!?

    6) why are you wasting memory doing string additions on static echo elements?

    7) why are you redefining $website inside the loop when it's static?

    8) this is 2014 not 1997, you have no business having the 'target' attribute on a site, much less shoving new windows down the user's throat with it.

    9) doesn't make a lot of sense for them all to be getting the same anchor... If you want to pass a unique value to them, you need to pass a unique value. In this case I'd do it using $_GET -- something like

    Code:
    <a href="', $website, '?row=', urlencode($row['V_ID'], '&amp;col=', urlencode($index), '&amp;value=', urlencode($row['head_flagger']), '">
    10) since those TD are all so much alike, I'd put them in a function.

    11) since you are only accessing them associative, use fetch_assoc not fetch_array (or pass the assoc restriction to it)

    So, let's drag that kicking and screaming into the light:

    Code:
    <?php
    
    $website = 'http://localhost/vchange.php';
    
    echo '
    	<table class="std">
    		<thead>
    			<tr>
    				<th scope="col">CLASS</th>
    				<th scope="col">HEAD FLAGGER</th>
    				<th scope="col">CORNER FLAGGER 1</th>
    				<th scope="col">CORNER FLAGGER 2</th>
    				<th scope="col">CENTER SCORER 1</th>
    				<th scope="col">CENTER SCORER 2</th>
    				<th scope="col">BOARD 1</th>
    				<th scope="col">BOARD 2</th>
    				<th scope="col">STAGER</th>
    			</tr>
    		</thead><tbody>';
    		
    function tdAnchor($index) {
    	global $row, $website;
    	echo '
    				<td>
    					<a href="', $website, '?row=', urlencode($row['V_ID'], '&amp;col=', urlencode($index), '&amp;value=', urlencode($row['head_flagger']), '">
    						', $row['head_flagger'], '
    					</a>
    				</td>';
    }
    
    $stmt = $con->query('SELECT * FROM volunteer');
     
    while ($row = $stmt->fetch_assoc()) {
    
    	echo '
    			<tr>
    				<td>', $row['class'], '</td>';
    				
    	tdAnchor('head_flagger');
    	tdAnchor('corner_one');
    	tdAnchor('corner_two');
    	tdAnchor('center_one');
    	tdAnchor('center_two');
    	tdAnchor('board_one');
    	tdAnchor('board_two');
    	tdAnchor('stager');
    	
    	echo '
    			</tr>';
    			
    }
    			
    echo '
    		</tbody>
    	</table>';
    	
    $con->close();
    
    ?>
    On your next page (vchange.php) $_GET['row'] == the $row['v_id'] they clicked on, $_GET['col'] is the name of the column / result index they clicked on, and $_GET['value'] would be the value of that cell.

    Hope this helps.
    Last edited by deathshadow; 10-31-2014 at 01:11 PM. Reason: missed $website as a global in function.

  8. #8
    Join Date
    Oct 2014
    Posts
    8
    Ok deathshadow, a little to absorb and I am going thru now to understand all that you are telling me - thanks!

    I copied it and am trying to use it, so I can better understand it, but there an error of an unexpected ';' at the end of the function that I am attempting to figure out now.

    Thanks for the lesson!

  9. #9
    Quote Originally Posted by Bigmo View Post
    I copied it and am trying to use it, so I can better understand it, but there an error of an unexpected ';' at the end of the function that I am attempting to figure out now.
    I missed a ) on one of the urlencode, what I get for typing it in the edit box here instead of a real editor.

    Code:
    echo '
    				<td>
    					<a href="', $website, 
    					'?row=', urlencode($row['V_ID']),
    					'&amp;col=', urlencode($index), 
    					'&amp;value=', urlencode($row['head_flagger']), '">
    					', $row['head_flagger'], '
    					</a>
    				</td>';
    Should do the trick. Sometimes splitting stuff into multiple lines like that makes it easier to find the error.

  10. #10
    Join Date
    Oct 2014
    Posts
    8
    Ok deathshadow, I am learning something here, my inexperience is making it slow...but thank you.

    I found the error too and they only issue I am having at this moment is that your code from the function is only using the data from 'head_flagger' and not the data from the other columns in the DB such as stager or any of the others you refer to in the function that are part of the DB.

    I see that value=urlencode($row['head_flagger' grabs the value of that field, though only in 'head_flagger'. and the data for all the columns is the data from 'head_flagger' only...

    So while I am understanding what you have done, I am not sure how to fix for each column. Separate functions is a lot of wasted time that you scolded me for before...

    Please point me a direction (hoping I made myself clear and what I need).

    Thanks for the teaching, I realize I have a long way to go!

  11. #11
    oops, that's another typo.

    Code:
    echo '
    				<td>
    					<a href="', $website, '?row=', urlencode($row['V_ID'], '&amp;col=', urlencode($index), '&amp;value=', urlencode($row[$index]), '">
    						', $row[$index], '
    					</a>
    				</td>';
    Though I'm not sure you even need to pass 'value' -- server side you should be probably using the database to pull the row again instead of sending the data back and forth to/from the server. Usually the less data you pass back and forth between the two the better. If you can get by with passing what row was clicked and what column was clicked, that should be all the info you need on the server.

    Really though this is treading into being so huge I wouldn't be doing this in the first place -- to what end is this being done? I think a lot of the mistakes I'm making trying to understand what you want comes from my having no clue what the data actually is and what you are trying to do with it.

  12. #12
    Join Date
    Oct 2014
    Posts
    8
    Thanks, I was real close, only reason I did not have that fix is I did not remove a set of ' '! IT works

    My goal here is to allow users to select a volunteer duty that is prefilled with 'XXX' then their registered number replaces the 'XXX' and we know they signed up for that duty. basically my thoughts were to have them click the 'XXX' in the row and column they want, the code checks to be sure that spot is still available, and changes it to their number. boom we know what they signed up for!

    I later had a thought that going to a another webpage is a waste, and the number could just toggle when they selected, so long as it is still available, and not open a new page, just refresh the current one. If they made a mistake they could click again and it toggles back to 'XXX'., but I have not put thought into how to accomplish that yet. They need to sign for 5 duties.

    Make sense?

  13. #13
    Ok, I get it. I would FIRST make it work as a normal sub web-page load, THEN enhance it with AJAX.

    All good JavaScript should enhance and already working page, instead of being "the only way it works" -- doing so is actually pretty easy and can speed development since you can get client-side and server-side working secure and flawless with just markup and PHP FIRST; then just have a little bit of PHP that does the same thing sending less data and scripting that just intercepts the anchor and plugs in the data.

    I'd put an ID on the TBODY, getElementsByTagName('a') on it to get all those nested anchors, detect if the anchor is calling "$website" and if so add onclick to it, AJAX the response to a different URI and cancel event bubbling.

    Don't know how familiar you are with JS and AJAX though.

    In any case, IF you want to go AJAX (which for updating just one field of many is a sane choice) ALWAYS try to make it work without AJAX FIRST, then enhance it. If nothing else it keeps the implementation logic simpler and lets you build in stages.

    Keeping the logic simple for the approval you could even just have the AJAX version of the update handler simply return true/false if the user gets that slot. (or even better, 0 or the user's name/id)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles