Page 1 of 1
Problems with for loops and if statements.
Posted: Sat Aug 06, 2011 2:01 am
by ThatPerson
I am in the process of creating something, which lists friend requests and has a 'Accept friend request' button. Due to the fact there can be any number of friend requests, I used a for loop to see if the button was pressed:
Code: Select all
$a = 0;
for ($i = 1; $i <= $len; $i++) {
$a = $a + 1;
$name = "submit".$a;
echo $name;
if (isset($_POST[$name])) {
$num = "$p".$a."pos";
echo $num;
$len = "$p".$a."len";
$test = mysql_query("select friendrequests from notes_notes where email = '$usr' and Password = '$pwd'") or die(mysql_error());
$test2 = substr_replace($test,"",${$num},${$len}+1);
echo $test2;
mysql_query("UPDATE notes_notes SET friendrequests = '$test2' WHERE email = '$usr' and Password = '$pwd'") or die(mysql_error());
$oldfriends = mysql_query("select friends from notes_notes where email = '$usr' and Password = '$pwd'") or die(mysql_error());
$old = $oldfriends.substr($fr,${"p".$a."pos"},${"p".$a."len"}).",";
echo $old;
mysql_query("UPDATE notes_notes SET friends = '$old' WHERE email = '$usr' and Password = '$pwd'") or die(mysql_error());
}
}
However this does not seem to work. Have I missed anything, or, if not, is there another way to do this?
Note: The text it is retrieving is in [text]$p1 $p2 $p1len $p2len $p1pos $p2pos[/text], and the buttons are [text]submit1,submit2,submit3[/text]
Re: Problems with for loops and if statements.
Posted: Sat Aug 06, 2011 2:54 am
by social_experiment
ThatPerson wrote:Due to the fact there can be any number of friend requests, I used a for loop to see if the button was pressed
Question: Clicking on a button means requesting a friend?
Re: Problems with for loops and if statements.
Posted: Sat Aug 06, 2011 3:37 am
by ThatPerson
Clicking the button means accepting a friend, I have a separate thing to send friend requests.
Also, I found that I was not using the form html thing when printing the buttons. I changed it and it worked slightly, but then failed again.
Re: Problems with for loops and if statements.
Posted: Sat Aug 06, 2011 3:47 am
by ThatPerson
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];
}
$usr = $_COOKIE['loggeduser'];
$pwd = $_COOKIE['loggedpass'];
$fr = mysql_fetch_object(mysql_query("select friendrequests from notes_notes where email = '$usr' and Password = '$pwd'")) -> friendrequests;
$user = mysql_query("select count(*) as count from notes_notes where email = '$usr' and Password = '$pwd'");
$user2 = mysql_fetch_object($user);
$user3 = $user2 -> count;
$oldOutPut = 0;
if ($user3 >= 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;
$outPut = $offset - $oldOutPut;
$friend = substr($fr,$oldOutPut,$outPut);
${$ppp} = $friend;
${$ppo} = 0;
${$ppl} = strlen($friend);
$oldOutPut = $offset;
$submit = "Submit".$p;
$cancel = "Cancel".$p;
echo <<<END
<div style="width:100%;height:10%;">
$friend
<br>
<FORM NAME ="form1" METHOD ="POST" ACTION = "friends.php">
<INPUT align="centre" TYPE = "Submit" Name = "$submit" VALUE = "Enter">
<input type="Submit" name='$cancel' 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 <= $len; $i++) {
$a = $a + 1;
$name = "cancel".$a;
if (isset($_POST[$name])) {
$num = "$p".$a."pos";
$len = "$p".$a."len";
$test = mysql_query("select friendrequests from notes_notes where email = '$usr' and Password = '$pwd'") or die(mysql_error());
$test3 = mysql_fetch_object($test);
$test4 = $test3 -> friendrequests;
$test2 = substr_replace(($test4.","),"",${$num},${$len}+1);
echo $test2;
mysql_query("UPDATE notes_notes SET friendrequests = '$test2' WHERE email = '$usr' and Password = '$pwd'") or die(mysql_error());
$tes = mysql_query("select friendrequests from notes_notes where email = '${'p'.$a}'") or die(mysql_error());
$tes3 = mysql_fetch_object($tes);
$tes4 = $tes3 -> friendrequests;
$tes2 = substr_replace(($tes4.","),"",${$num},${$len}+1);
mysql_query("UPDATE notes_notes SET friendrequests = '$tes2' WHERE email = '${'p'.$a}'") or die(mysql_error());
}
}
$a = 0;
for ($i = 1; $i <= $len; $i++) {
$a = $a + 1;
$name = "submit".$a;
if (isset($_POST[$name])) {
$num = "$p".$a."pos";
$len = "$p".$a."len";
$test = mysql_query("select friendrequests from notes_notes where email = '$usr' and Password = '$pwd'") or die(mysql_error());
$test3 = mysql_fetch_object($test);
$test4 = $test3 -> friendrequests;
$test2 = substr_replace(($test4.","),"",${$num},${$len}+1);
echo $test2;
mysql_query("UPDATE notes_notes SET friendrequests = '$test2' WHERE email = '$usr' and Password = '$pwd'") or die(mysql_error());
$tes = mysql_query("select friendrequests from notes_notes where email = '${'p'.$a}'") or die(mysql_error());
$tes3 = mysql_fetch_object($tes);
$tes4 = $tes3 -> friendrequests;
$tes2 = substr_replace(($tes4.","),"",${$num},${$len}+1);
mysql_query("UPDATE notes_notes SET friendrequests = '$tes2' WHERE email = '${'p'.$a}'") or die(mysql_error());
$oldfriends = mysql_query("select friends from notes_notes where email = '$usr' and Password = '$pwd'") or die(mysql_error());
$oldfriends2 = mysql_fetch_object($oldfriends);
$oldfriends3 = $oldfriends2 -> friends;
$oldfriend = mysql_query("select friends from notes_notes where email = '${'p'.$a}'") or die(mysql_error());
$oldfriend2 = mysql_fetch_object($oldfriend);
$oldfriend3 = $oldfriend2 -> friends;
$old = $oldfriends3.substr($fr,${"p".$a."pos"},${"p".$a."len"}).",";
$older = $oldfriend3.substr($fr,${"p".$a."pos"},${"p".$a."len"}).",";
mysql_query("UPDATE notes_notes SET friends = '$older' WHERE email = '${'p'.$a}'") or die(mysql_error());
mysql_query("UPDATE notes_notes SET friends = '$old' WHERE email = '$usr' and Password = '$pwd'") or die(mysql_error());
}
}
?>
That is the new code, and it has gone back to neither of the buttons working. No error messages, nothing.
Re: Problems with for loops and if statements.
Posted: Sat Aug 06, 2011 4:29 am
by ThatPerson
Right, I more or less fixed it, however this code gives out a weird answer. It should go from:
friends friend requests
Jeremy,Bethany,
to
friends friend requests
Jeremy, Bethany,
yet instead it goes one character at a time:
y
my
emy
remy
eremy
Jeremy.
Does anyone have any way to fix this?
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];
}
$usr = $_COOKIE['loggeduser'];
$pwd = $_COOKIE['loggedpass'];
$fr = mysql_fetch_object(mysql_query("select friendrequests from notes_notes where email = '$usr' and Password = '$pwd'")) -> friendrequests;
$user = mysql_query("select count(*) as count from notes_notes where email = '$usr' and Password = '$pwd'");
$user2 = mysql_fetch_object($user);
$user3 = $user2 -> count;
$oldOutPut = 0;
if ($user3 >= 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;
$outPut = $offset - $oldOutPut;
$friend = substr($fr,$oldOutPut,$outPut);
${$ppp} = $friend;
${$ppo} = 0;
${$ppl} = strlen($friend);
$oldOutPut = $offset;
$submit = "Submit".$p;
$cancel = "Cancel".$p;
echo <<<END
<div style="width:100%;height:10%;">
$friend
<br>
<FORM NAME ="form1" METHOD ="POST" ACTION = "friends.php">
<INPUT align="centre" TYPE = "Submit" Name = "$submit" VALUE = "Enter">
<input type="Submit" name='$cancel' 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 <= $len; $i++) {
$a = $a + 1;
$name = "Cancel".$a;
if (isset($_POST[$name])) {
$num = "$p".$a."pos";
$len = "$p".$a."len";
$test = mysql_query("select friendrequests from notes_notes where email = '$usr' and Password = '$pwd'") or die(mysql_error());
$test3 = mysql_fetch_object($test);
$test4 = $test3 -> friendrequests;
$test2 = substr_replace(($test4.","),"",${$num},${$len}+1);
echo $test2;
mysql_query("UPDATE notes_notes SET friendrequests = '$test2' WHERE email = '$usr' and Password = '$pwd'") or die(mysql_error());
$tes = mysql_query("select friendrequests from notes_notes where email = '${'p'.$a}'") or die(mysql_error());
$tes3 = mysql_fetch_object($tes);
$tes4 = $tes3 -> friendrequests;
$tes2 = substr_replace(($tes4.","),"",${$num},${$len}+1);
mysql_query("UPDATE notes_notes SET friendrequests = '$tes2' WHERE email = '${'p'.$a}'") or die(mysql_error());
}
}
$a = 0;
for ($i = 1; $i <= $len; $i++) {
$a = $a + 1;
$name = "Submit".$a;
if (isset($_POST[$name])) {
$num = "$p".$a."pos";
$len = "$p".$a."len";
echo ${$len};
echo ${$num};
$test = mysql_query("select friendrequests from notes_notes where email = '$usr' and Password = '$pwd'") or die(mysql_error());
$test3 = mysql_fetch_object($test);
$test4 = $test3 -> friendrequests;
$test2 = substr_replace($test4,"",${$num},${$len}+1);
echo $test2;
mysql_query("UPDATE notes_notes SET friendrequests = '$test2' WHERE email = '$usr' and Password = '$pwd'") or die(mysql_error());
$tes = mysql_query("select friendrequests from notes_notes where email = '${'p'.$a}'") or die(mysql_error());
$tes3 = mysql_fetch_object($tes);
$tes4 = $tes3 -> friendrequests;
$tes2 = substr_replace(($tes4),"",${$num},${$len});
mysql_query("UPDATE notes_notes SET friendrequests = '$tes2' WHERE email = '${'p'.$a}'") or die(mysql_error());
$oldfriends = mysql_query("select friends from notes_notes where email = '$usr' and Password = '$pwd'") or die(mysql_error());
$oldfriends2 = mysql_fetch_object($oldfriends);
$oldfriends3 = $oldfriends2 -> friends;
$oldfriend = mysql_query("select friends from notes_notes where email = '${'p'.$a}'") or die(mysql_error());
$oldfriend2 = mysql_fetch_object($oldfriend);
$oldfriend3 = $oldfriend2 -> friends;
$old = $oldfriends3.substr($fr,${"p".$a."pos"},${"p".$a."len"});
echo $old;
$older = $oldfriend3.substr($fr,${"p".$a."pos"},${"p".$a."len"});
echo $older;
mysql_query("UPDATE notes_notes SET friends = '$older' WHERE email = '${'p'.$a}'") or die(mysql_error());
mysql_query("UPDATE notes_notes SET friends = '$old' WHERE email = '$usr' and Password = '$pwd'") or die(mysql_error());
}
}
?>