Jump to content

Counting Commas


lpxxfaintxx

Recommended Posts

[code]$sql2="SELECT profilehits FROM fusion_users WHERE user_name = '$owner'";
$result2=mysql_query($sql2);
$rows2=mysql_fetch_array($result2);
$views = $rows2['profileviews'];
$arr = explode(",", $views);
[/code]

I thought it'd work but it just shows up as 1. What am I doing wrong?

Thanks
Link to comment
Share on other sites

Put in some debugging code to dump the $rows2 array after the fetch. I would also use the mysql_fetch_assoc() function instead of the mysql_fetch_array() function.

Sample debug code:
[code]<?php
$sql2="SELECT profilehits FROM fusion_users WHERE user_name = '$owner'";
$result2=mysql_query($sql2);
$rows2=mysql_fetch_assoc($result2);
echo '<pre>' . print_r($rows2,true) . '</pre>';  // debug code
?>[/code]

If this doesn't show what you expect, go back to the code that stored the information in the database and see if there is an error there.

Please post what it shows.

Ken
Link to comment
Share on other sites

I did what you wrote, I think... but it's doing something odd.

[code]<?php
$owner = $_GET['id'];
require_once "db.php";



$sql2="SELECT profilehits FROM fusion_users WHERE user_name = '$owner'";
$result2=mysql_query($sql2);
$rows2=mysql_fetch_assoc($result2);
echo '<pre>' . print_r($rows2,true) . '</pre>';  // debug code
$views = $rows2['profileviews'];
$arr = explode(",", $views);
$number=count($arr);

?>
This profile is hosted by <a href="http://aimmultimedia.com">AIM Multimedia.com</a> <br>
<?php echo $number; ?>
<hr>
[/code]

If I view the profileheader.php alone, it comes out as "1" [a href=\"http://aimmultimedia.com/profileheader.php\" target=\"_blank\"]http://aimmultimedia.com/profileheader.php[/a] but if I do an 'include' it shows the whole array, but still a 1. (Still a problem) [a href=\"http://aimmultimedia.com/viewprofile.php?id=lpxxfaintxx\" target=\"_blank\"]http://aimmultimedia.com/viewprofile.php?id=lpxxfaintxx[/a]

Thanks for the help.
Link to comment
Share on other sites

Ok, now I tried this:

[code]<?php
$owner = $_GET['id'];
require_once "db.php";



$sql2="SELECT profilehits FROM fusion_users WHERE user_name = '$owner'";
$result2=mysql_query($sql2);
$rows2=mysql_fetch_assoc($result2);
echo '<pre>' . print_r($rows2,true) . '</pre>';  // debug code
$views = $rows2['profileviews'];
$arr = explode(",", $views);
$number=count($arr);

?>
This profile is hosted by <a href="http://aimmultimedia.com">AIM Multimedia.com</a> <br>
<?php echo '<pre> ' . print_r($rows2,true) . '</pre>';  ?>
<hr>[/code]


but it comes out as blank. [a href=\"http://aimmultimedia.com/profileheader.php\" target=\"_blank\"]http://aimmultimedia.com/profileheader.php[/a]
Link to comment
Share on other sites

what about?

$sql2="SELECT profilehits FROM fusion_users WHERE user_name = '$owner'";
$result2=mysql_query($sql2);
$rows2=mysql_fetch_array($result2);
$views = $rows2['profileviews'];
$arr = explode(",", $views);
$=0;
foreach($arr as $count) {
$i++;
}
echo $i;


You tryed that?
Link to comment
Share on other sites

Sorry should have been (typo)

[code]
$sql2="SELECT profilehits FROM fusion_users WHERE user_name = '$owner'";
$result2=mysql_query($sql2);
$rows2=mysql_fetch_array($result2);
$views = $rows2['profileviews'];
$arr = explode(",", $views);
$i=0;
foreach($arr as $count) {
$i++;
}
echo $i;
[/code]

If thats not doin it then try

[code]
$sql2="SELECT profilehits FROM fusion_users WHERE user_name = '$owner'";
$result2=mysql_query($sql2);
$rows2=mysql_fetch_array($result2);
$views = $rows2['profileviews'];
echo('before explode value is: '.$views.'<Br>')
$arr = explode(',', $views);
$i=0;
foreach($arr as $count) {
$i++;
echo('Comma: '.$i.' Value:'.$count.'<Br>');
}
[/code]


This will help you by seeing origonal data, data being counted and so on..
Link to comment
Share on other sites

[!--quoteo(post=364332:date=Apr 13 2006, 07:01 PM:name=shocker-z)--][div class=\'quotetop\']QUOTE(shocker-z @ Apr 13 2006, 07:01 PM) [snapback]364332[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[code]
$sql2="SELECT profilehits FROM fusion_users WHERE user_name = '$owner'";
$result2=mysql_query($sql2);
$rows2=mysql_fetch_array($result2);
$views = $rows2['profileviews'];
echo('before explode value is: '.$views.'<Br>')
$arr = explode(',', $views);
$i=0;
foreach($arr as $count) {
$i++;
echo('Comma: '.$i.' Value:'.$count.'<Br>');
}
[/code]
This will help you by seeing origonal data, data being counted and so on..
[/quote]

I think mysql_fetch_array() should read mysql_fetch_assoc() if I'm not mistaken.
Link to comment
Share on other sites

I think the problem is occuring in the script where you enter the data into the database, not in the script that looks at it.

Let's start again...

Please post the script that loads the data, including any forms that might be involved, then tell us how you want the data to be displayed.

Ken
Link to comment
Share on other sites

Ok, the table is set up like this:

CREATE TABLE `fusion_users` (
`user_id` smallint(5) unsigned NOT NULL auto_increment,
`user_name` varchar(30) NOT NULL default '',
`user_password` varchar(32) NOT NULL default '',
`user_email` varchar(100) NOT NULL default '',
`user_hide_email` tinyint(1) unsigned NOT NULL default '1',
`user_location` varchar(50) NOT NULL default '',
`user_birthdate` date NOT NULL default '0000-00-00',
`user_aim` varchar(16) NOT NULL default '',
`user_icq` varchar(15) NOT NULL default '',
`user_msn` varchar(100) NOT NULL default '',
`user_yahoo` varchar(100) NOT NULL default '',
`user_web` varchar(200) NOT NULL default '',
`user_theme` varchar(100) NOT NULL default 'Default',
`user_offset` char(3) NOT NULL default '0',
`user_avatar` varchar(100) NOT NULL default '',
`user_sig` text NOT NULL,
`user_posts` smallint(5) unsigned NOT NULL default '0',
`user_joined` int(10) unsigned NOT NULL default '0',
`user_lastvisit` int(10) unsigned NOT NULL default '0',
`user_ip` varchar(20) NOT NULL default '0.0.0.0',
`user_rights` text NOT NULL,
`user_groups` text NOT NULL,
`user_level` tinyint(3) unsigned NOT NULL default '101',
`user_status` tinyint(1) unsigned NOT NULL default '0',
[b] `profilehits` text NOT NULL, [/b]
`lastview` varchar(20) NOT NULL default '',
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;




This is a sample export of what a row might look like:

INSERT INTO `fusion_users` VALUES (1, 'lpxxfaintxx', 'b572a1b0e48edb7405ab55feb2673', 'lpxxfaintxx@yahoo.com', 1, '', '0000-00-00', '', '', '', '', '', 'Default', '0', '', '', 2, 1143371157, 1144947993, '71.247.236.84', 'A.AC.AD.B.C.CP.DB.DC.D.FQ.F.IM.I.IP.M.N.NC.P.PH.PI.PO.S.SL.S1.S2.S3.S4.S5.S6.S7.SU.UG.U.W.WC', '.1', 103, 0, '[b]0lil alx9d1, BagFullOfRic, YOURMOM, fdndfnfd , fdndfnfd , fdndfnfd, fdndfnfd, fdndfnfd, fdndfnfd, sleeperpick10, BagFullOfRice, BagFullOfRice74, iix3JAD0REuU, iix3JAD0REuU, sinwillsin, sportzr4me18, sleeperpick10, BaSkEtBaLLQT8674, xpjisuuus, BagFullOfShyt, %n, %n, BagFullOfRice, Patelk0014, Patelk0014, BagFullOfRice, BagFullOfRice, , Patelk0014, flirtyazngrl09, bigshot9214, bigshot9214, bigshot9214, mnkmnkz16, bleausky22312, , , fashnFrEEk92, BagFullOfRice, BagFullOfRice, , xbsbdfbdbfdbfd, xbsbdfbdbfdbfd, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, , BagFullOfRice, %n, BagFullOfRice, %n, %n, , BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, %n, BagFullOfRice, BagFullOfRice, %n, BagFullOfRice, %n, BagFullOfRice, %n, BagFullOfShyt, BagFullOfShyt, BagFullOfShyt, rubberducky9314, rubberducky9314, rubberducky9314, %n, %n, BagFullOfRice, BagFullOfRice, BagFullOfRice, %n, Patelk0014, rubberducky9314, bigshot9214, bigshot9214, %n, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice, BagFullOfRice[/b]', '');


The bold is the whole "Profile Hits" part, so there should be at least 50 commas, not one.


The whole page where I'm trying to echo out the commas is:

[code]
<?php
$owner = $_GET['id'];
require_once "db.php";



$sql2="SELECT profilehits FROM fusion_users WHERE user_name = '$owner'";
$result2=mysql_query($sql2);
$rows2=mysql_fetch_assoc($result2);


$sql2="SELECT profilehits FROM fusion_users WHERE user_name = '$owner'";
$result2=mysql_query($sql2);
$rows2=mysql_fetch_assoc($result2);
$views = $rows2['profileviews'];
$arr = explode(",", $views);
#$=0;
foreach($arr as $count) {
$i++;
}


?>
This profile is hosted by <a href="http://aimmultimedia.com">AIM Multimedia.com</a> <br>
Become a BETA tester like me! <a href="http://aimmultimedia.com/application.php">Sign up here.</a><br>
<?php echo 'The profile has been viewed '.$i.' times.'; ?>
<hr>

[/code]


This is where the profilehits is getting updated and added


<?php
require_once "db.php";
require_once "profileheader.php";
$owner = $_GET['id'];
[b]$visitor = $_GET['sn'];[/b]
$status = 'Public';

[b]$idq = mysql_query("SELECT `profilehits` FROM `fusion_users` WHERE `user_name` = '$owner'");
$ida = mysql_fetch_assoc($idq);
$newid = $ida['profilehits'] . ', '.$visitor;

$sql = "UPDATE fusion_users SET profilehits='$newid' WHERE user_name = '$owner'";
$result = mysql_query($sql);

[/b]
$sql = "SELECT * FROM `registered_cat` WHERE `owner` = '$owner' AND `status` = '$status'";
$result=mysql_query($sql);
$num_rows = mysql_num_rows($result);
echo 'There are '.$num_rows.' categories. Please choose one.<br><br>';
while($row=mysql_fetch_array($result)){
{
$id = $row['id'];
echo '<HTML><FONT FACE="Verdana" SIZE=4><A HREF="/viewprofilecat.php?id='.$id.'&sn=%n" TARGET="_self"><B>'.$row['cat_name'].'</B></A></FONT> <br>' .
"Description : {$row['cat_description']} <br>" .
"Total Images : {$row['id']} <br> <hr>";

}
}
?>




Sorry about the no code tags, I wanted to make the important parts more standing out.

Again, thanks for the help.
Link to comment
Share on other sites

Found your problem.

The field in the table is named "profilehits", the field you're trying to get is "profileviews". Change "profileviews" to "profilehits" and it will work.

BTW, you don't need the "foreach" loop to count the number of commas. After you do the explode, just use the count() function on the array.

[code]<?php
$sql2="SELECT profilehits FROM fusion_users WHERE user_name = '$owner'";
$result2=mysql_query($sql2);
$rows2=mysql_fetch_assoc($result2);
$views = $rows2['profileviews'];
$arr = explode(",", $views);
?>
This profile is hosted by <a href="http://aimmultimedia.com">AIM Multimedia.com</a> <br>
Become a BETA tester like me! <a href="http://aimmultimedia.com/application.php">Sign up here.</a><br>
<?php echo 'The profile has been viewed '. count($arr) .' times.'; ?>[/code]

Another BTW --- if you change how you're storing the field "profilehits" from a CSV to a serialized array, it will make your scripting easier.

In the scripts where the field gets updated:
[code]<?php
$idq = mysql_query("SELECT `profilehits` FROM `fusion_users` WHERE `user_name` = '$owner'");
$ida = mysql_fetch_assoc($idq);
$profilehits = ($ida['profilehits'] == '')?array():unserialize($ida['profilehits']);
$profilehits[] = $visitor;

$sql = "UPDATE fusion_users SET profilehits='" . serialize($profilehits) . "' WHERE user_name = '$owner'";
$result = mysql_query($sql);
?>[/code]
Then in the "counting" script:
[code]<?php
$sql2="SELECT profilehits FROM fusion_users WHERE user_name = '$owner'";
$result2=mysql_query($sql2);
$rows2=mysql_fetch_assoc($result2);
echo 'This profile is hosted by <a href="http://aimmultimedia.com">AIM Multimedia.com</a> <br>';
echo 'Become a BETA tester like me! <a href="http://aimmultimedia.com/application.php">Sign up here.</a><br>';
echo 'The profile has been viewed '. count(serialize($rows2['profilehits'])) .' times.';
?>[/code]

Ken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.