How To Add More Than One Entry On A Single Cell/Array Where Each Entry Is Separated By Commas?

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 same single cell or mysql row.
The script tries monitoring what you are browsing via the:


and then record your viewed urls 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 
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";			
			}				
		}
	}
}

?>

Sample 2

<?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";			
			}				
		}
	}
}

?>

First off, I think your database design is flawed. If I read correctly, you’re building up a single MySQL column which will contain multiple URLs that your user has provided, and you’ll separate them with commas. What do you intend to do with that data once you have it? I suspect you’ll find that whatever it is, it would have been much easier if you stored a separate row for each URL, with some method of ordering them, even something as simple as the date/time they were accessed.

One flaw in the code you have is that you’re not actually separating them with commas in either version of your code, which I suspect is just giving you a syntax error. If you display the insert/update query and then try to run it directly in phpmyadmin, does it work? I think one would look like:

// new url is www.sitepoint.com
// history is www.microsoft.com
UPDATE users SET browsings = 'www.sitepoint.com''www.microsoft.com' WHERE usernames = '".$user."'";

I think those single-quotes will upset things, and where is your comma?

Other than that, though, what’s the question? Is it not working, and if not, in what way (unless the above covers it)?

Thanks for the reply.
I first did add a comma and coded a variety of different ways and they all throwed error. The only 2 that didn’t throw error is what I listed in this thread but as you can see the entries are not being separated by commas. I get no error with these 2 codes, though.
Yes, you have figured correctly what I want to do. But, I’ll explain again and you can be kind enough to show a few example codes. :wink:

EXAMPLE
Imagine you viewed 1.com now on your first session. 1.com would be listed on the following columns:

latest_browsings
browsings (full history).

So now, the columns would look like this:

BROWSINGS | LATEST_BROWSINGS
1.com | 1.com

Now, if you move-onto 2nd.com then that would be UPDATED on the latest_browsings column but INSERTED in the browsings column.
So now, the columns would look like this:

BROWSINGS|LATEST_BROWSINGS |
1.com | 2.com
2.com|

Note the “Browsings” column show your full browsing history (like your browser shows you when you click CTRL+H. The “latest_browsings” shows you the most current page you viewed (like a Status Update thingy in social network accounts. So, “latest_browsings” only show data from position zero. No other rows get filled).
Now, how would you code to achieve that ? Where should I make the change ?

Thanks in advance! :slight_smile:

I would change the ‘browsings’ table so that it is organised like this:

username
datetime
URL

and each time you want to add something to the history, you just insert a new record with the appropriate timestamp on it. You can then retrieve them in whatever order you need, and do other stuff that will be difficult if you just stick them all in the same column with a comma, a vertical-bar or any other separator character.

You can also see how long ago the user visited the URL, which can be handy if you only want to keep a certain amount of history. A simple query will delete anything older than your arbitrary age limit.

1 Like

Thanks for understanding my aim. Actually, I already did few mnths earlier what you suggested.
I already have an .exe bot that has a TIMESTAMP column. Now trying to build the web version and I would add the TIMESTAMP on that too. But the reason for having 2 columns:

Browsings
Latest_View

Is because as soon as you login to your account, you will see 2 iframes. One on the left and one on the right.
The left one would show you the latest page you friend viewed. The right iframe would show all row data from the BROWSINGS (full history) column (like google SERP) so you can trace your friends website visits. So, as you can see, to make things easier to deal with the iframe and all, I need 2 columns so each iframe opens to their respective columns (so to speak).

I have a feeling you are now gonna tell me to just program the left iframe to open to the final row (BROWSINGS) and forget the Latest_View column. Am I right ?

A programmer told me (another subject) to use the following code to get the script to spit out the final row of the column:

SELECT * FROM users
ORDER BY id DESC
LIMIT 1

Do you have any suggestions yourself ? Remember, his code suggestion was not related to this thread and so might not be too good (ORDER by id) for this thread’s subject.
I asked for a code sample how to get script to show only final record.

Thanks man!

I did start to suggest that, but it will all depend on how many records are in the table per user. If you keep thousands of history records for each username, in theory it will take longer to select them all, order them (though in this case you’d order them by your timestamp column) and then display the final one. It might not be a measurable amount of time.

Are they really 2 columns, or are they two separate tables? Your code suggests that these are separate tables. Terminology is important, it can get confusing if you use the wrong terms. When I say you should not put all the individual URLs in the same column separated by a comma, what I’m saying is wrong would be to have a database laid out like this:

username
browsing_history

where on the first entry, you have this single row:

fred
www.url.com

and after the second entry, you then have:

fred
www.microosft.com, www.url.com

and then

fred
www.sitepoint.com, www.microosft.com, www.url.com

What I think you should do would have the first entry

fred
2017/04/10 18:00:00
www.url.com

then you add another row

fred
2017/04/10 18:10:00
www.microosft.com

and then another one

fred
2017/04/10 18:15:00
www.sitepoint.com

meaning you end up with a table like this:

username     timestamp          url
fred       2017/04/10 18:00:00  www.url.com
fred       2017/04/10 18:10:00  www.microosft.com
fred       2017/04/10 18:15:00  www.sitepoint.com

and not a table like this

username    browsing
fred        "www.sitepoint.com, www.microosft.com, www.url.com"

It’s a little more work to retrieve the URLs my way, but far more flexible.

Yes, you are correct. My .exe bot shows data like this:

username timestamp url
droopsnot 2017/04/10 18:00:00 www.url.com
ui man 2017/04/10 18:10:00 www.microosft.com
droopsnot 2017/04/10 18:15:00 www.sitepoint.com

Actually, the columns were:
TimeStamp|Id|Username|Browsing_Histories|Contact_Details
(Ofcourse, if any public tries contacting any user (like you) then they’d have to go through filling-in a cpa survey before they see your contact details. Thus earn you $1-20 cpa earning.
Bear in mind, this is one tbl with many cols and not more than one tbl. “Browsings_History” tbl that shows all users browsing history on a single webpage. It is like a SERP with GO TO NEXT PAGE facility. Imagine a google serp.

But I NOW want my web version of the bot or member site/social network (sn) to show like this:

Username | Browsing_Histories |
droopsnot | “www.sitepoint.com, www.microosft.com, www.url.com
ui man | “www.sitepoint.com, www.facebook.com, www.google.com

Originally, I wanted the SN to show like this:

Username | Browsing_Histories | Latest_Viewings
droopsnot | “www.sitepoint.com, www.microosft.com, www.url.com” | www.url.com
ui man | “www.sitepoint.com, www.facebook.com, www.google.com” | www.google.com

That way, the left iframe shows you your friends’ Latest_Viewings column, row 0.
And the right iframe shows you all your friends browsing histories (Browsing_Histories) column.

Anyway, let me experiment and decide how it should display the data to the user. Right now, I need to learn how to add more than one entry on the same cell/array separated by commas. The code I learn might become handy in future on other projects where it is necessary to have more than one entry on a single cell/array and it is right and recommended to do so like that.
A cell is called array, right ? We say array in php and cell in ms excell. Correct ?
I did once download a youtube tut that showed how to code to dump many entries into a single cell. I might aswell see if I can find it.

No, cells and arrays are different things. A cell in a spreadsheet contains an entry that might be a string, or a number, or a formula or whatever. I don’t think (but am not an expert in PHP) that anything is described as a ‘cell’ in PHP.

An array, on the other hand, is a group of values. So it would be more like a ‘range’ in Excel. Arrays are a useful way of grouping data, for example if you want to pass many values into a function, it can be easier to do that by passing an array than having lots of individual parameters.

That’s fine, learning is always good, but just keep in mind that it’s not a great thing to do. I am struggling to think of why this would ever be a good thing to do, or the “right and recommended” way to handle it. Have a read up on “Database normalisation” and think about how you deal with your single column containing many comma-separated values if you suddenly have a need to display them in alphabetic order, or in reverse order, or any other order than you stored them in. Consider how big this column will need to be, to store any meaningful number of URLs.

How you want the display to appear isn’t really all that relevant to the issue here. You can easily produce the display you want now by storing individual entries rather than shoving them into a single database column.

1 Like

Mmm. I thought ARRAY was equivalent to a cell we have in MS excell.

If I were make a loose comparison I’d say an array is like a row or column, one being the array name the other being the key, and a cell is more like an array value.

... A .. B .. C 
J . r .. s .. t
K . u .. v .. w 
L . x .. y .. z 

and

$A['J'] == 'r' 
$A['K'] == 'u' 
$C['L'] == 'z' 
... or 
$J['B'] == 's' 
$K['C'] == 'w' 

etc.

1 Like

Quite right - a single element in an array is more like a cell in a spreadsheet. You could think of the sheet itself as an array:

$sheet['A1'] = 100;
$sheet['A2'] = 200;
$sheet['B1'] = .45;
          A          B
------------------------
1|       100        0.45
2|       200  

and so on, if I’m not just writing the same as @mittineague in another form.

2 Likes

So, either like a row or a column but not both ?

An excell sheet contains both columns & rows. Arrays contain only one or the other ?
Let’s look at mysql tbl here:

Usernames|Passwords
droopsnoot|pass
mittineague|password

Now, from the above example, which part is ARRAY and which part is associative array ? We always come across:
mysqli fetch array
mysqli fetch assoc

I think on one youtube tut they said if you want only 1 cell (so to speak in terms of ms excell) you use assoc array. Like …

while($row = mysqli_fetch_assoc($result))

Else “array” to get the whole column data. Like …

while($row = mysqli_fetch_array($result))

Maybe I understood wrong ? (I hope I coded my samples correctly. Writing at the top of my head. In a sense).

Not really. A single-dimension array contains only a row or a column (doesn’t really matter which), whereas a two-dimensional array contains rows and columns, if you want to visualise them that way. So a two-dimensional array might contain five rows, each with ten columns, if that’s what you want it to.

No, that’s just not correct. You can easily see that’s not correct by just calling var_dump on the return from either of those statements. The first returns an associative array, where each element is referenced by the column name for the next row in the result set, and the data is the column content itself. The second returns either exactly the same thing in the same format, or the same thing as an indexed array, or I think there’s an option to return both. You’d need to use mysqli_fetch_all() to get an entire column or columns.

2 Likes

That is excellent but when you got free time in your hand then kindly show 3 visual/img/video examples of the 3 things you mentioned. Or, any link that shows the difference would do.

Thanks!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.