For loop issues

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
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

For loop issues

Post by ThatPerson »

Code: Select all

<?php
include("database.php");
$usr = $_COOKIE['loggeduser'];
$pass = $_COOKIE['loggedpass'];
$fr = mysql_fetch_object(mysql_query("select friendrequests from notes_notes where email = '$usr' and Password = '$pass'")) -> friendrequests;

$p = 0;
$offset = 0;
$len = strlen($fr);
for ($i = 1; $i <= $len; $i++) {

if (strpos($fr,",",$offset) != false) {
$offset = strpos($fr,",",$offset)+1;
$p = $p + 1;
}
}



if ($fr != "") {
echo <<<END
<div>
<input type="Submit" name="Friendslist" value="Friend Requests (".$p.")">
</div>
END;
}
?>
I am having a problem with this code as during the for loop it does not tell how many friend requests the person has. In the table I have it set out like:
[text]Bethany,Jeremy,[/text]
(Not real names on the website, but to show the format), so it should find two commas, and display two. Yet instead, it makes a button saying
[text]Friend Requests ([/text]

Can anybody see any errors here or a way to get error messages from it?
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: For loop issues

Post by Dodon »

change

Code: Select all

<input type="Submit" name="Friendslist" value="Friend Requests (".$p.")">
 
into

Code: Select all

<input type="Submit" name="Friendslist" value="Friend Requests ($p)">
 
You closed the value tag by using ".$p." inside the button value tag
Last edited by Dodon on Fri Aug 05, 2011 4:00 am, edited 2 times in total.
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Re: For loop issues

Post by ThatPerson »

Thanks, I replaced it with your line of code and it ran perfectly. Now it says that I have 2 friend requests, which is right.
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Re: For loop issues

Post by ThatPerson »

Also, as a continuation of this, I have another problem (I did not want to post another thread, this way all of the problems with my friend request code are in one place).

Code: Select all

<?php
include("database.php");
function findString($startPoint, $endPoint, $source) {
    $m=array();
    preg_match_all('/' . preg_quote($startPoint, '/') . '([^)]+)'. preg_quote($endPoint, '/').'/i', $source, $m);
    return $m[1][0];
}
$fr = mysql_fetch_object(mysql_query("select friendrequests from notes_notes where email = '$usr' and Password = '$pass'")) -> friendrequests;
$usr = $_COOKIE['loggeduser'];
$pwd = $_COOKIE['loggedpass'];
$user = mysql_query("select count(*) as count from notes_notes where email = '$usr' and Password = '$pwd'");
$oldOutPut = 0;

if ($user >= 1) {
if ($fr != "") {
 $p = 0;
 $offset = 0;
 $len = strlen($fr);
 $ppp = "p".$p;
 $ppl = "p".$p."len";
 $ppo = "p".$p."pos";
 for ($i = 1; $i <= $len; $i++) {
  if (strpos($fr,",",$offset) != false) {
   $offset = strpos($fr,",",$offset)+1;
   $p = $p + 1;
   
    $friend = findString($oldOutPut,$offset,$fr);
    ${$ppp} = $friend;
    ${$ppo} = 0;
    ${$ppl} = strlen($friend);
    $oldOutPut = $offset;
echo <<<END
<div style="width:100%;height:10%;">
$friend
<input type="Submit" name="submit".$p value="Add as friend">
<input type="Submit" name="cancel".$p value="Not now">
</div>
END;
   }
  }
 }

else {
echo <<<END
<div style = "width:50%;height:100%">
Sorry, you have no friend requests
</div>
END;

}
}
$a = 0;
for ($i = 1; $i <= $p; $i++) {
 $a = $a + 1;
 $name = "submit".$a;
  if (isset($_POST[$name])) {
   $num = "$p".$a."pos";
   $len = "$p".$a."len";
   substr_replace($fr,"",$num,$len+1);
   $oldfriends = mysql_query("select friends from notes_notes where email = '$usr' and Password = '$pwd'");
   $old = $oldfriends.findString($num,$num+$len,$fr);
   mysql_query("UPDATE notes_notes SET friends = '$old' WHERE email = '$usr' and Password = '$pass'");
  }
}
?>
The code above *should* check if you have any friend requests, if so create a for loop which sets the variables $p(1,2,3,4,5,6,7), $p(1,2,3,4,5,6,7)len, $p(1,2,3,4,5,6,7)pos so they can be recalled later to update the button being pressed. However, even though the code in the other problem I had here detects that in the friend requests there is text, this fails to so does not post the correct message. Can anybody see any errors in this code. Also, findString is a function I made which should get the text between two points in a string.
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: For loop issues

Post by Dodon »

What is the exact issue that occurs? Does it show "Sorry, you have no friend requests" or something else? What do you expect it to show?
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Re: For loop issues

Post by ThatPerson »

It returns 'Sorry, you have no friend requests' , yet the friendrequests column has text in. It should return 'Jeremy: Bored and Tired here. Accept Friend request'

Where 'Jeremy' is the username, 'Bored and Tired here' is the description and 'Accept Friend Request' is a button which removes the data from the friend request column and adds it to the friends column.
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: For loop issues

Post by Dodon »

well if $fr is empty "if ($fr != "")" it'll say you don't have friend requests, what does $fr contain?

do a var_dump($fr);die(); right before the if statement.
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Re: For loop issues

Post by ThatPerson »

Ah, now I see, it returns NULL, which poses the question of how come the $fr line worked on the other piece of code.
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Re: For loop issues

Post by ThatPerson »

I worked out the problem. I was setting $usr and $pass after $fr yet $fr used $usr and $pass.

But still, the issue remains, it still comes out as NULL. Any ideas? The code has now :

Code: Select all

$usr = $_COOKIE['loggeduser'];
$pwd = $_COOKIE['loggedpass'];
$fr = mysql_fetch_object(mysql_query("select friendrequests from notes_notes where email = '$usr' and Password = '$pass'")) -> friendrequests;
Last edited by ThatPerson on Fri Aug 05, 2011 4:46 am, edited 1 time in total.
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: For loop issues

Post by Dodon »

Took a second look

Code: Select all

$fr = mysql_fetch_object(mysql_query("select friendrequests from notes_notes where email = '$usr' and Password = '$pass'")) -> friendrequests;
$usr = $_COOKIE['loggeduser'];
$pwd = $_COOKIE['loggedpass'];
 
You define $usr and $pwd after the query... so the query will probably end up finding no records so $fr == ""

second

Code: Select all

$user = mysql_query("select count(*) as count from notes_notes where email = '$usr' and Password = '$pwd'");
 
if ($user >= 1) { 
 
will always succeed even if there are no matches with the username and password.

edit:
Seems you posted the solution yourself while I was typing :)
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Re: For loop issues

Post by ThatPerson »

The $user issue is not really as important as this is for a control I will be including onto the main page. I changed the problems you said about, yet it still returns NULL.

Edit: Never mind, I realized it was checking the Password against $pass when it should be $pwd.
Post Reply