#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0

    PHP-MySQL question


    Hello everyone, I am trying to create a search function online on a members database. I am using php-mysql. The code below shows the result of my search function which is the member's personal information. I use index.php to write all the php code function and use member.html.php to show the webpage.

    Question 1: I would like to view full information of that particular member when I click the member name. For this code I wrote, I can only view the last member info for all the button I click. I think because the ID stop at the last one in the for loop. How can I improve it?

    Question 2: If I click the Phone Number button below, php will collect all the phone number that selected by the checkbox and show it on the new webpage. How can I improve it?

    Any help would be much appreciated. Thank you very much.

    Remarks: htmlout = htmlspecialchars function

    Code:
    <table><form action="?" method="post">
    	 <?php foreach ($rows as $row): ?>
    	 <tr>             
    	  <td width="300"><input type = "hidden" name = "id" value = "<?php htmlout($row['ID']); ?>">
    	                  <input type = "hidden" name = "action" value = "viewprofile">
    	                  <input type = "submit" value = "<?php htmlout($row['Name']) ?>"></td>				  
          <td width="300"><fieldset>
    		<legend>Phone</legend>
    		  
    			<div><label for="row<?php htmlout($row['Phone']);?>">
    			     <input type="checkbox" name="selectedphone[]"
    						id="row<?php htmlout($row['Phone']); ?>" value="<?php htmlout($row['Phone']); ?>"><?php htmlout($row['Phone']); ?></label></div>
    			
    	    </fieldset></td>
          <td width="300"><?php htmlout($row['Currentstatus']) ?></td>
          <td width="300"><?php htmlout($row['Gender']) ?></td>
          <td width="300"><?php htmlout($row['Race']) ?></td>
          <td width="300"><?php htmlout($row['Nationality']) ?></td>
          <td width="400"><?php htmlout($row['Birthday']) ?></td>
          <td width="550"><?php htmlout($row['Address']) ?></td>
          <td width="400"><?php htmlout($row['University']) ?></td>
          <td width="400"><?php htmlout($row['Coursemajor']) ?></td>
    	 </tr>
    	 <?php endforeach; ?>
    	 </form>
    	 <tr>
    	  <form action="?" method="post">
    	  <td><div align="center"><input type = "hidden" name = "action" value = "phonenumber">
    							  <input type = "submit" value = "Phone Number"></div></td>
    	  </form>
    	 <td>
    	  <form action="?" method="post">
    	  <div align="center"><input type = "hidden" name = "action" value = "anothersearch">
    						  <input type = "submit" value = "Another Search"></div>
    	  </form>
         </td>
    	 </tr>
        </table>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,992
    Rep Power
    533
    Hello,

    Question 1. Instead of a form, use a simple link to the detail record where the users id is included in the URL. Also, typically, when you just want to get some data, you should use GET and when you want to permanently change or add some data, you use POST. A link by its very nature uses GET, so you don't even need the form.

    Question 2: I would get rid of your phone ID, and just use user ID. When submitting the form to the server (using GET again), your server will take the users IDs and return the appropriate phone numbers.

    Also, see if you can get rid of all the nested forms and just use one. Many of them will not work since you are not including the inputs which you need sent to the server within them (unless you are using JavaScript to move them, but it still can be improved).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    The way I go with what this sounds is usually work as 2 files, but just cuz mine aren't too basic to start. The first/main page has the listing. I usually allow to sort by column, as well as live search/filter of the table. I then make each row highlighted onMouseOver, and is a link to the second file.

    I usually name it profile.php because that's what details usually qualify as. Within this one, I usually have it starting with a switch listing, and the file is built with a table where each item is an if/else statement. Upon first load, this page is in 'view' mode by default. This will display the detail of the selected content laid out as desired. The switches meerly control the view/add/edit/remove modes.

    All formats for my scripts usually use same layout.
    View just lays content out to view.
    Add offers blank input boxes.
    Edit will take the View items you see, and make text boxes appear around the words.
    Remove is meerly to delete the line from the database. Remember to ask for confirmation before deleting something.
    Last edited by Triple_Nothing; June 4th, 2013 at 09:36 AM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0

    Sorry, not very clear


    Hi, thanks for everyone reply.

    To NotionCommotion,

    I am not sure whether you know the function I want.
    Maybe I still very new in php. I explain my question again.
    The scenario is I have a member database.
    After login, it will shows the search member webpage which I have done so.
    After I search, it will shows a member list who meet the search criteria on the next page.
    This page is the page I posted yesterday.
    From this page, I only need 2 functions.
    First, when I click the member name button, it will shows the full profile of that particular member on the other page (I have done this page).
    For this function, I just do not know how to get the respective member id.
    For second function, I want to choose the members' phone number who I want to contact and shows it on the new page.
    For this function, I do not know how to gather the selected phone number and shows it on the new page.
    Hope everyone is understand my scenario.

    To Triple_Nothing,

    I just learnt php 1 month ago, can you give some hints? Thanks.

    Thanks everyone once again.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,992
    Rep Power
    533
    AhHong,

    Do as I indicated in my first post. I will provide you some more detail...

    For your first question, don't use a form. Instead, create a list of users like:
    Code:
    <ul>
      <li><a href="detail.php?id=123">John Doe</a></li>
      <li><a href="detail.php?id=321">Jane Doe</a></li>
      <li><a href="detail.php?id=222">Baby Doe</a></li>
    </ul>
    Obviously, you can create whatever HTML you want (i.e. a table) to display the list. Then in your PHP, use $id=$_GET['id'], query your database, and display whatever you want.


    For your second question, create a simple form which sends the users ID via GET (not POST) to the server, and then have your server query the DB and return the appropriate HTML. Note that I used the name user_id[] with the open/closed brackets "[]". This will send your data as an array.
    Code:
    <form action="phonenumbers.php" method="get">
        <input type="checkbox" name="user_id[]" value="65">(555) 444-3333<br /> 
        <input type="checkbox" name="user_id[]" value="72">(444) 444-3333<br />
        <input type="checkbox" name="user_id[]" value="93">(888) 444-3333<br />     
        <input name="submit" type="submit" value="submit">
    </form>
    In your phonenumbers.php file, before doing anything, test the data transfer using the following script. What do you see?
    PHP Code:
    if(isset($_GET['user_id'])){echo('<pre>'.print_r($_GET['user_id'],1).'</pre>');} 

    Comments on this post

    • AhHong agrees
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    First, when I click the member name button, it will shows the full profile of that particular member on the other page (I have done this page).
    For this function, I just do not know how to get the respective member id.
    For second function, I want to choose the members' phone number who I want to contact and shows it on the new page.
    For this function, I do not know how to gather the selected phone number and shows it on the new page.
    From the way you are making this sound is the search already offers you a list, and you can click an item on this list to view the full profile.

    1.) Have you already accomplished clicking an item to view the profile, or is this still a goal?
    2.) If you have accomplished #1, does this page not already displayed the individual's phone number, and offer you the userid?
    3.) If you haven't done any more selecting other than the general search, which the mention of not knowing how to fetch the id is offering, then...

    When you build your initial list from the search, select the member ID as well, and include it in your link to the profile page. The link will be roughly similar to href="?mode=view&user=143" This creates 2 variables in your address bar as written, and can be called via $_GET[].

    $_GET['mode'] //Which will equal 'view'
    $_GET['user'] //Which will equal the user id '143'


    EDIT: Ah, finally clicked on the phone number. You said 2nd function for initial page, n I was thinking after you clicked the user to view the profile. My bad. NotionCommotion laid that out rather nicely.
    Last edited by Triple_Nothing; June 5th, 2013 at 07:20 AM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    7
    Rep Power
    0
    Hi, thank you everyone for the help!
    I used the 'get' method instead of 'post' method.
    The function work well.
    I edited the code which shown below:

    Code:
    <table><form action="?" method="get">
    	 <?php foreach ($rows as $row): ?>
    	 <tr>             
    	  <td width="300"><a href="index.php?id=<?php htmlout($row['ID']); ?>"><?php htmlout($row['Name']) ?></a></td>				  
          <td width="300"><fieldset>
    		<legend>Phone</legend>
    		  
    			<div><label for="row<?php htmlout($row['Phone']);?>">
    			     <input type="checkbox" name="selectedphone[]"
    						id="row<?php htmlout($row['Phone']); ?>" value="<?php htmlout($row['Phone']); ?>"><?php htmlout($row['Phone']); ?></label></div>
    			
    	    </fieldset></td>
          <td width="300"><?php htmlout($row['Currentstatus']) ?></td>
          <td width="300"><?php htmlout($row['Gender']) ?></td>
          <td width="300"><?php htmlout($row['Race']) ?></td>
          <td width="300"><?php htmlout($row['Nationality']) ?></td>
          <td width="400"><?php htmlout($row['Birthday']) ?></td>
          <td width="550"><?php htmlout($row['Address']) ?></td>
          <td width="400"><?php htmlout($row['University']) ?></td>
          <td width="400"><?php htmlout($row['Coursemajor']) ?></td>
    	 </tr>
    	 <?php endforeach; ?>
    	 <tr>
    	  <td><div align="center"><input type = "hidden" name = "action" value = "phonenumber">
    							  <input type = "submit" value = "Phone Number"></div></td>
    	  </form></table>
  14. #8
  15. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0
    $result = mysql_query("SELECT * FROM KeepScores");


    $fields_num = mysql_num_fields($result);

    echo "<table><tr>";
    // printing table headers


    echo "<td>Recent Posts</td>";

    echo "</tr>\n";

    while($row = mysql_fetch_row($result))
    {
    echo "<tr>";


    foreach($row as $cell)
    echo "<td>$cell</td>";

    echo "</tr>\n";
    }
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Well, since this got bumped back up, I figure I'd toss a rough example on how I build all the tables on my site. Every table's contents get built with the exact same function, other than what it is requesting from DB to post.

    Code:
                          <DIV id="outer">
                          <DIV id="innera">
                            <script src="includes/sorttable.js"></script>
                            <TABLE id="table" class="sortable">
                              <colgroup>
                                <col width="290">
                                <col width="160">
                                <col width="110">
                                <col width="170">
                                <col width="90">
                                <col width="250">
                                <col width="115">
                              </colgroup>
                              <THEAD>
                                <TR>
                                  <TH width="290">Company</TH>
                                  <TH width="160">Name</TH>
                                  <TH width="110">Position</TH>
                                  <TH width="170">Number</TH>
                                  <TH width="90">Type</TH>
                                  <TH width="250">E-Mail</TH>
                                  <TH width="115">Notes</TH>
                                </TR>
                              </THEAD>
                              <TBODY>
    <?php buildListing(); ?>
                              </TBODY>
                            </TABLE>
                          </DIV>
                          </DIV>
    PHP Code:
    function buildListing() {
      global 
    $link;
      
    $statement $link->prepare("SELECT `Company`, `Name`, `Position`, `Number`, `Type`, `E-mail`, `Note`, `ID` FROM `rolodex` ORDER BY `Company`");
      
    $statement->execute();
      
    $statement->bind_result($col1,$col2,$col3,$col4,$col5,$col6,$col7,$col8);
      while (
    $statement->fetch()) {
        echo 
    '                            <TR class="highlightable" href="?loc=rolodex&action=view&id=' $col8 '"><TD>' $col1 '</TD><TD>' $col2 '</TD><TD>' $col3 '</TD><TD>' $col4 '</TD><TD>' $col5 '</TD><TD>' $col6 '</TD><TD>' $col7 "</TD></TR>\n";
      }
      
    $statement->close();


IMN logo majestic logo threadwatch logo seochat tools logo