Jump to content


Photo

Counting Commas


  • Please log in to reply
19 replies to this topic

#1 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 08 April 2006 - 09:49 PM

$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 thought it'd work but it just shows up as 1. What am I doing wrong?

Thanks

#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 08 April 2006 - 10:32 PM

$number=count($arr);
gives in your case the number of elements exploded by [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--],[!--colorc--][/span][!--/colorc--]

#3 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 08 April 2006 - 10:59 PM

Nope, still 1. Could it be the MySQL query?

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 April 2006 - 05:45 AM

What does your data look like? What do you expect to be in the array element $rows2['profileviews']?

Ken

#5 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 09 April 2006 - 07:58 PM

I expect it to look like Anounymous, user1, myname, lpxxfaintxx, tommy, etc.

#6 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 10 April 2006 - 01:05 PM

Any ideas?

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 April 2006 - 02:24 PM

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

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

#8 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 10 April 2006 - 07:11 PM

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

<?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>

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.

#9 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 11 April 2006 - 11:31 AM

Ok, now I tried this:

<?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>


but it comes out as blank. [a href=\"http://aimmultimedia.com/profileheader.php\" target=\"_blank\"]http://aimmultimedia.com/profileheader.php[/a]

#10 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 12 April 2006 - 11:03 AM

I've tried everything I can, but still no.

#11 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 12 April 2006 - 11:19 AM

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?
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#12 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 12 April 2006 - 05:54 PM

What should I do for the "$=0;" part of your script?

edit: I tried the script without it, and again its just "1". This is driving me mad!

#13 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 13 April 2006 - 09:01 AM

Sorry should have been (typo)

$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;

If thats not doin it then try

$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>');
}


This will help you by seeing origonal data, data being counted and so on..
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#14 BladeMetal

BladeMetal
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 13 April 2006 - 10:27 AM

[!--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) View Post[/div][div class=\'quotemain\'][!--quotec--]
$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>');
}
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.


#15 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 13 April 2006 - 05:09 PM

Still "1".

#16 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 April 2006 - 07:16 PM

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

#17 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 13 April 2006 - 08:50 PM

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',
`profilehits` text NOT NULL,
`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, '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', '');


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:

<?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>



This is where the profilehits is getting updated and added


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

$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);


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

#18 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 15 April 2006 - 11:45 AM

[img src=\"http://www.phpfreaks.com/forums/style_images/1/folder_post_icons/icon9.gif\" border=\"0\" alt=\"IPB Image\" /]

#19 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 15 April 2006 - 12:50 PM

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.

<?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.'; ?>

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:
<?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);
?>
Then in the "counting" script:
<?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.'; 
?>

Ken

#20 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 15 April 2006 - 04:12 PM

Ah, I feel so stupid now. Thanks a bunch.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users