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
    • 919
    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:

    <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.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:

    [php]
    <?php 
    session_start();
    require "conn.php";
    require "site_details.php";
    
    /*Check if user is logged-in or not by checking if session is set or not. 
    If user is not logged-in then redirect to login page. Else, show user's account homepage.php.*/
    
    if(!isset($_SESSION["user"])) 
    {
        header("location:login.php");
    }
    else 
    {
        $user = $_SESSION["user"];
        ?>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <title>Browse!</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>
        <body>
        <p>
        <p>
        <p>    
        <?php 
        //Display 'Browser' ?>
        <iframe src='<?php echo $db_latest_view;?>'></iframe>
        <p>
        <p>
        <p>
        <form method="post" action="">
        <table border="1" width="50%">
        <tr>
        <td width="10">Url: </td>
        <td><input type="text" name="browse_url" size="120"></td>
        </tr>
        <tr>
        <td width="10">Browse: </td>
        <td>
        <select name="browsing_type">
        <OPTION>Anonymous Browsing</OPTION>
        <OPTION>Group Browsing</OPTION>
        </SELECT>
        </td>
        </tr>
        <td></td>
        <td><input type="submit" name="browse" size="50" value="Browse"><input type="submit" name="search_keywords" size="50" value="Search Keywords"></td>
        <tr>
        <td width="10">Message: </td><td><textarea name="message" cols="120" rows="10"></textarea></td>
        </tr>
        <tr>
        <td></td>
        <td width="50"><input type="submit" name="submit_message" size="50" value="Send Message!"></td>
        </tr>
        <p>
        <p>
        </table>
        </form>
        
        <?php 
        if(isset($_REQUEST['browse'])) 
        {
            $browse_url = trim(strip_tags(strtolower(mysqli_real_escape_string($conn,$_POST["browse_url"]))));
            $browsing_type = trim(strip_tags(strtolower(mysqli_real_escape_string($conn,$_POST["browsing_type"]))));
            
            //Grab User details from database.
            $sql = "SELECT * FROM users WHERE usernames = '".$user."'";    
            $result = mysqli_query($conn,$sql);
            $numrows = mysqli_num_rows($result);
            if($numrows) 
            {    
                while($row = mysqli_fetch_assoc($result))
                {
                    $db_user_browsings = $row["browsings"];                
                }
                
                $sql = "INSERT INTO users(browsings) VALUES('".$browse_url."''".$db_user_browsings."')";
                $result = mysqli_query($conn,$sql);
                if($sql)
                {
                    echo "true";            
                }
                
                $sql = "UPDATE users SET browsings_latest = '".$browse_url."' WHERE usernames = '".$user."'";
                $result = mysqli_query($conn,$sql);
                if($sql)
                {
                    echo "true";            
                }                
            }
        }
    }
    
    ?>
    [/php]

     

    Sample 2

    [php]
    
    <?php 
    session_start();
    require "conn.php";
    require "site_details.php";
    
    /*Check if user is logged-in or not by checking if session is set or not. 
    If user is not logged-in then redirect to login page. Else, show user's account homepage.php.*/
    
    if(!isset($_SESSION["user"])) 
    {
        header("location:login.php");
    }
    else 
    {
        $user = $_SESSION["user"];
        ?>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <title>Browse!</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>
        <body>
        <p>
        <p>
        <p>    
        <?php 
        //Display 'Browser' ?>
        <iframe src='<?php echo $db_latest_view;?>'></iframe>
        <p>
        <p>
        <p>
        <form method="post" action="">
        <table border="1" width="50%">
        <tr>
        <td width="10">Url: </td>
        <td><input type="text" name="browse_url" size="120"></td>
        </tr>
        <tr>
        <td width="10">Browse: </td>
        <td>
        <select name="browsing_type">
        <OPTION>Anonymous Browsing</OPTION>
        <OPTION>Group Browsing</OPTION>
        </SELECT>
        </td>
        </tr>
        <td></td>
        <td><input type="submit" name="browse" size="50" value="Browse"><input type="submit" name="search_keywords" size="50" value="Search Keywords"></td>
        <tr>
        <td width="10">Message: </td><td><textarea name="message" cols="120" rows="10"></textarea></td>
        </tr>
        <tr>
        <td></td>
        <td width="50"><input type="submit" name="submit_message" size="50" value="Send Message!"></td>
        </tr>
        <p>
        <p>
        </table>
        </form>
        
        <?php 
        if(isset($_REQUEST['browse'])) 
        {
            $browse_url = trim(strip_tags(strtolower(mysqli_real_escape_string($conn,$_POST["browse_url"]))));
            $browsing_type = trim(strip_tags(strtolower(mysqli_real_escape_string($conn,$_POST["browsing_type"]))));
            
            //Grab User details from database.
            $sql = "SELECT * FROM users WHERE usernames = '".$user."'";    
            $result = mysqli_query($conn,$sql);
            $numrows = mysqli_num_rows($result);
            if($numrows) 
            {    
                while($row = mysqli_fetch_assoc($result))
                {
                    $db_user_browsings = $row["browsings"];                
                }
                
                $sql = "UPDATE users SET browsings = '".$browse_url."''".$db_user_browsings."' WHERE usernames = '".$user."'";
                $result = mysqli_query($conn,$sql);
                if($sql)
                {
                    echo "true";            
                }    
                
                $sql = "UPDATE users SET browsings_latest = '".$browse_url."' WHERE usernames = '".$user."'";
                $result = mysqli_query($conn,$sql);
                if($sql)
                {
                    echo "true";            
                }                
            }
        }
    }
    
    ?>
    
    [/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
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: