2 PHP Questions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

2 PHP Questions

Post 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?
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: 2 PHP Questions

Post 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)?
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: 2 PHP Questions

Post 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.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: 2 PHP Questions

Post by Celauran »

Didn't realize you were on a Windows machine. I think you'll still need to set recurse to TRUE.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: 2 PHP Questions

Post by Pazuzu156 »

Tried. It threw no error, but didn't create the directory.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: 2 PHP Questions

Post 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.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: 2 PHP Questions

Post by Celauran »

You're using assignment ($i = 1) instead of comparison ($i == 1)
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: 2 PHP Questions

Post by Pazuzu156 »

I fixed it. Now it says 2 Connects2 Connects
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: 2 PHP Questions

Post 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.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: 2 PHP Questions

Post 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";
    }
}
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: 2 PHP Questions

Post 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";
}
?>
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: 2 PHP Questions

Post 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
Post Reply