Page 1 of 1
2 PHP Questions
Posted: Fri Nov 18, 2011 1:14 pm
by Pazuzu156
So, I have 2 questions that need solved.
1) When using the mkdir() command, i get the error: Directory not found! This is my code:
Code: Select all
<?php
mkdir('/images/ppic/uname',0,false);
?>
uname is the name of the user that is created so they can store their images.
2) How exactly do i count the number of values within 1 cell of a row for mysql?
Say i have a table, that table has 1 row for me, but the other rows are for other users. In my row under the friends, there's 5 names separated by a , how to i print out the number of names in that cell?
Re: 2 PHP Questions
Posted: Fri Nov 18, 2011 1:23 pm
by Celauran
Pazuzu156 wrote:So, I have 2 questions that need solved.
1) When using the mkdir() command, i get the error: Directory not found! This is my code:
Code: Select all
<?php
mkdir('/images/ppic/uname',0,false);
?>
Remove the leading slash and change the mode. 0666, 0777, 0755, whatever. 0 will give you no rwx permissions for anyone. You'll also need to set recursive argument to TRUE for nested directories.
Pazuzu156 wrote:2) How exactly do i count the number of values within 1 cell of a row for mysql?
Say i have a table, that table has 1 row for me, but the other rows are for other users. In my row under the friends, there's 5 names separated by a , how to i print out the number of names in that cell?
explode() + count($array)?
Re: 2 PHP Questions
Posted: Fri Nov 18, 2011 1:42 pm
by Pazuzu156
Celauran wrote:Remove the leading slash and change the mode. 0666, 0777, 0755, whatever.
mode is ignored on windows machines. And thanks for the answer to the second question.
Re: 2 PHP Questions
Posted: Fri Nov 18, 2011 1:43 pm
by Celauran
Didn't realize you were on a Windows machine. I think you'll still need to set recurse to TRUE.
Re: 2 PHP Questions
Posted: Fri Nov 18, 2011 1:46 pm
by Pazuzu156
Tried. It threw no error, but didn't create the directory.
Re: 2 PHP Questions
Posted: Fri Nov 18, 2011 3:14 pm
by Pazuzu156
To improve my second question, here is the code I have:
Code: Select all
<?php
$con = mysql_result($query,0,'connects');
$exp = explode(', ',$con);
if(!empty($con)) {
for($i=0;$i<count($exp);$i++) {
if($i=1) {
echo "1 Connect";
} else {
echo $i." Connects";
}
}
} else {
echo "0 Connects";
}
?>
But it either says 0 Connects when it's empty or 1 Connect. It won't count all the values in the cell.
Re: 2 PHP Questions
Posted: Fri Nov 18, 2011 3:17 pm
by Celauran
You're using assignment ($i = 1) instead of comparison ($i == 1)
Re: 2 PHP Questions
Posted: Fri Nov 18, 2011 3:25 pm
by Pazuzu156
I fixed it. Now it says 2 Connects2 Connects
Re: 2 PHP Questions
Posted: Fri Nov 18, 2011 3:31 pm
by Pazuzu156
Seems like it's displaying the number how ever many times it finds a value. I only want that to echo once. Would I break; it?
UPDATE
======
Nvm, I answered my own question. Thanks for the help anyhow.
Re: 2 PHP Questions
Posted: Fri Nov 18, 2011 3:35 pm
by Celauran
I can't duplicate the problem. This works fine for me:
Code: Select all
$query = "SELECT connects FROM test WHERE id = 1";
list($result) = $sql->query($query)->fetch_row();
$exp = explode(", ", $result);
for ($i = 0; $i < count($exp); $i++)
{
if ($i == 1)
{
echo "1 Connect";
}
else
{
echo $i . " Connects";
}
}
Re: 2 PHP Questions
Posted: Fri Nov 18, 2011 3:39 pm
by Pazuzu156
I just used count($exp) to get the number
Code: Select all
<?php
$con = mysql_result($query,0,'connects');
$exp = explode(', ',$con);
if(!empty($con)) {
$c = " Connect";
if(count($exp)==1) {
echo count($exp).$c;
} else {
echo count($exp).$c.'s';
}
} else {
echo "0 Connects";
}
?>
Re: 2 PHP Questions
Posted: Sat Nov 19, 2011 2:37 pm
by califdon
Pazuzu156 wrote:2) How exactly do i count the number of values within 1 cell of a row for mysql?
Say i have a table, that table has 1 row for me, but the other rows are for other users. In my row under the friends, there's 5 names separated by a , how to i print out the number of names in that cell?
MySQL is a relational database. Relational databases do not have "cells". Relational databases should be designed in accordance with the Relational Model, which states rules that govern tables and their rows and columns (what you referred to as "cells"). One primary rule states that a column cannot contain multiple values, which is what you described. When you have such a situation, it calls for a
related table, such as this:
Code: Select all
+--------------+ +--------------+
| tblUsers | | tblFriends |
+--------------+ +--------------+
| uID |<--+ | fID |
| name | +--| uID |
+--------------+ +--------------+
When properly designed, a relational database permits using queries to return unlimited relationships, which the design you described cannot do. A good explanation of this can be found at:
http://www.tonymarston.co.uk/php-mysql/ ... l#relation