Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How To Add More Than One Entry On A Single Cell/Array Where Each Entry Is Separated By Commas?

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 958
    Answer it

    Programming buddies,

    I'm back online after nearly 2wks of offline. Gonna be harassing you guys again and more this time. Eeek!

    Anyway, right now, I'm trying to build a script that adds multi-entries into the same single cell MySQLsql row.
    The script tries monitoring what you are browsing via the:

    1. <input type="text" name="browse_url" size="120">

    and then record your viewed url's into the same row (position: 0), column: browsings like so:

    1.com,2.com and so on.
    So, at first, the mysql array or cell is blank. When you view a url (eg.) 1.com then the array would show like this:

    1.com

    And then afterwards, if you view facebook.com then the cell should get updated by first grabbing the previously viewed urls and then adding the latest url onto the same cell/array like so (each url separated by comma):

    1. 1.com,facebook.com

    Throw your precious eyes on line 79 onwards on both sample scripts. I reckon the 1st script is no good but the 2nd should work. Gave you both scripts to show the variety of ways I attempted.


    Sample 1:

    1. [php]
    2. <?php
    3. session_start();
    4. require "conn.php";
    5. require "site_details.php";
    6.  
    7. /*Check if user is logged-in or not by checking if session is set or not.
    8. If user is not logged-in then redirect to login page. Else, show user's account homepage.php.*/
    9.  
    10. if(!isset($_SESSION["user"]))
    11. {
    12. header("location:login.php");
    13. }
    14. else
    15. {
    16. $user = $_SESSION["user"];
    17. ?>
    18. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    19. <html>
    20. <head>
    21. <title>Browse!</title>
    22. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    23. </head>
    24. <body>
    25. <p>
    26. <p>
    27. <p>
    28. <?php
    29. //Display 'Browser' ?>
    30. <iframe src='<?php echo $db_latest_view;?>'></iframe>
    31. <p>
    32. <p>
    33. <p>
    34. <form method="post" action="">
    35. <table border="1" width="50%">
    36. <tr>
    37. <td width="10">Url: </td>
    38. <td><input type="text" name="browse_url" size="120"></td>
    39. </tr>
    40. <tr>
    41. <td width="10">Browse: </td>
    42. <td>
    43. <select name="browsing_type">
    44. <OPTION>Anonymous Browsing</OPTION>
    45. <OPTION>Group Browsing</OPTION>
    46. </SELECT>
    47. </td>
    48. </tr>
    49. <td></td>
    50. <td><input type="submit" name="browse" size="50" value="Browse"><input type="submit" name="search_keywords" size="50" value="Search Keywords"></td>
    51. <tr>
    52. <td width="10">Message: </td><td><textarea name="message" cols="120" rows="10"></textarea></td>
    53. </tr>
    54. <tr>
    55. <td></td>
    56. <td width="50"><input type="submit" name="submit_message" size="50" value="Send Message!"></td>
    57. </tr>
    58. <p>
    59. <p>
    60. </table>
    61. </form>
    62. <?php
    63. if(isset($_REQUEST['browse']))
    64. {
    65. $browse_url = trim(strip_tags(strtolower(mysqli_real_escape_string($conn,$_POST["browse_url"]))));
    66. $browsing_type = trim(strip_tags(strtolower(mysqli_real_escape_string($conn,$_POST["browsing_type"]))));
    67. //Grab User details from database.
    68. $sql = "SELECT * FROM users WHERE usernames = '".$user."'";
    69. $result = mysqli_query($conn,$sql);
    70. $numrows = mysqli_num_rows($result);
    71. if($numrows)
    72. {
    73. while($row = mysqli_fetch_assoc($result))
    74. {
    75. $db_user_browsings = $row["browsings"];
    76. }
    77. $sql = "INSERT INTO users(browsings) VALUES('".$browse_url."''".$db_user_browsings."')";
    78. $result = mysqli_query($conn,$sql);
    79. if($sql)
    80. {
    81. echo "true";
    82. }
    83. $sql = "UPDATE users SET browsings_latest = '".$browse_url."' WHERE usernames = '".$user."'";
    84. $result = mysqli_query($conn,$sql);
    85. if($sql)
    86. {
    87. echo "true";
    88. }
    89. }
    90. }
    91. }
    92.  
    93. ?>
    94. [/php]

     

    Sample 2

    1. [php]
    2.  
    3. <?php
    4. session_start();
    5. require "conn.php";
    6. require "site_details.php";
    7.  
    8. /*Check if user is logged-in or not by checking if session is set or not.
    9. If user is not logged-in then redirect to login page. Else, show user's account homepage.php.*/
    10.  
    11. if(!isset($_SESSION["user"]))
    12. {
    13. header("location:login.php");
    14. }
    15. else
    16. {
    17. $user = $_SESSION["user"];
    18. ?>
    19. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    20. <html>
    21. <head>
    22. <title>Browse!</title>
    23. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    24. </head>
    25. <body>
    26. <p>
    27. <p>
    28. <p>
    29. <?php
    30. //Display 'Browser' ?>
    31. <iframe src='<?php echo $db_latest_view;?>'></iframe>
    32. <p>
    33. <p>
    34. <p>
    35. <form method="post" action="">
    36. <table border="1" width="50%">
    37. <tr>
    38. <td width="10">Url: </td>
    39. <td><input type="text" name="browse_url" size="120"></td>
    40. </tr>
    41. <tr>
    42. <td width="10">Browse: </td>
    43. <td>
    44. <select name="browsing_type">
    45. <OPTION>Anonymous Browsing</OPTION>
    46. <OPTION>Group Browsing</OPTION>
    47. </SELECT>
    48. </td>
    49. </tr>
    50. <td></td>
    51. <td><input type="submit" name="browse" size="50" value="Browse"><input type="submit" name="search_keywords" size="50" value="Search Keywords"></td>
    52. <tr>
    53. <td width="10">Message: </td><td><textarea name="message" cols="120" rows="10"></textarea></td>
    54. </tr>
    55. <tr>
    56. <td></td>
    57. <td width="50"><input type="submit" name="submit_message" size="50" value="Send Message!"></td>
    58. </tr>
    59. <p>
    60. <p>
    61. </table>
    62. </form>
    63. <?php
    64. if(isset($_REQUEST['browse']))
    65. {
    66. $browse_url = trim(strip_tags(strtolower(mysqli_real_escape_string($conn,$_POST["browse_url"]))));
    67. $browsing_type = trim(strip_tags(strtolower(mysqli_real_escape_string($conn,$_POST["browsing_type"]))));
    68. //Grab User details from database.
    69. $sql = "SELECT * FROM users WHERE usernames = '".$user."'";
    70. $result = mysqli_query($conn,$sql);
    71. $numrows = mysqli_num_rows($result);
    72. if($numrows)
    73. {
    74. while($row = mysqli_fetch_assoc($result))
    75. {
    76. $db_user_browsings = $row["browsings"];
    77. }
    78. $sql = "UPDATE users SET browsings = '".$browse_url."''".$db_user_browsings."' WHERE usernames = '".$user."'";
    79. $result = mysqli_query($conn,$sql);
    80. if($sql)
    81. {
    82. echo "true";
    83. }
    84. $sql = "UPDATE users SET browsings_latest = '".$browse_url."' WHERE usernames = '".$user."'";
    85. $result = mysqli_query($conn,$sql);
    86. if($sql)
    87. {
    88. echo "true";
    89. }
    90. }
    91. }
    92. }
    93.  
    94. ?>
    95.  
    96. [/php]

     

 1 Answer(s)

  • Hi
    As from your need you want to append the new browse url to existing url, which is a string.
    So when you have already get the prev browse url of user in $db_user_browsings = $row["browsings"];
    Again append it with new one and it will go like this:-
    $db_user_browsings =$db_user_browsings.','.$browse_url;
    Now $db_user_browsings is only var you have to update.
    cheers!
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: