Schlo_50 Posted December 4, 2007 Share Posted December 4, 2007 Hello phpfreaks, I am trying to pass a unique variable through a URL into my display.php page so that i can then use it to match and print out all associated information from my flat file database. So far my script isn't working in that there is just a blank page, no error messages so there must be simple something i am missing. I'll firstly show you the function to pass the variable and then the page which matches the id and prints the relevant information. function: function view($id){ $lines = file("usercats.txt"); foreach ($lines as $line) { $data[$key] = explode("|", $line); $user = trim($Data[$Key][0]); $catname = trim($Data[$Key][1]); $id = trim($Data[$Key][2]); $_SESSION['id'] = $id; print '<a href="display.php?id='.$_SESSION['id'].'">$catname</a><br />'; } } display.php <?php if(isset($_GET['id'])){ $id = (int) $_GET['id']; //type casting - only want integers for our ID. This would prevent any SQL injection attack }else{ echo 'The Category information could not be found!'; exit; } $file = file("usercats.txt"); foreach($file as $Key => $Val){ $Data[$Key] = explode("|", $Val); $loop_id = $Data[$Key][1]; if($loop_id == $id){//each time the loop runs, check if it is the ID we are looking for if it is display the info $username = $Data[$Key][0]; $thumbid = $Data[$Key][2]; $title = $Data[$Key][3]; $desc = $Data[$Key][4]; $date = $Data[$Key][5]; //print out the information print $username; print $loop_id; print $thumbid; print $title; print $desc; print $date; break;//once found, we can exit out of our foreach loop } } ?> Forgive e if i am doing something stupid, i have been practising using functions and flat files for not very long. Any help , comments or snippets would be most appreciated! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/ Share on other sites More sharing options...
GingerRobot Posted December 4, 2007 Share Posted December 4, 2007 Try changing these two lines: $file = file("usercats.txt"); foreach($file as $Key => $Val){ to: $Data = file("usercats.txt"); foreach($Data as $Key => $Val){ You use the $Data variable inside you loop, but you called it $file. Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-405907 Share on other sites More sharing options...
Schlo_50 Posted December 4, 2007 Author Share Posted December 4, 2007 There is still a blank page with no errors nor information? Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-405922 Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 print_r($Data) to check if there's anything there to begin with... PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-405923 Share on other sites More sharing options...
Schlo_50 Posted December 4, 2007 Author Share Posted December 4, 2007 Array ( [0] => Array ( [0] => dom [1] => 291107020533 ) [1] => Array ( [0] => [1] => 1196432585.jpg [2] => Motion [3] => ym,y, [4] => 30/11/07 ) ) was the returned result. Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-405928 Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 Is that what you expected to see? Just to be sure, are we now using this code? <?php if(isset($_GET['id'])){ $id = (int) $_GET['id']; //type casting - only want integers for our ID. This would prevent any SQL injection attack }else{ echo 'The Category information could not be found!'; exit; } $Data = file("usercats.txt"); foreach($Data as $Key => $Val){ $Data[$Key] = explode("|", $Val); $loop_id = $Data[$Key][1]; //etc If anything in the code has changed but that, please re-post the revised code. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-405959 Share on other sites More sharing options...
Schlo_50 Posted December 4, 2007 Author Share Posted December 4, 2007 That is a the current revised code that you posted. And that is what i expected to see, but when i take it away there still, is nothing printed.. ??? Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-405983 Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 Let's do this: <?php foreach($Data as $Key => $Val){ echo $Key . ' => ' . $Val . '<br>'; } die(); Post results PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-405985 Share on other sites More sharing options...
Schlo_50 Posted December 4, 2007 Author Share Posted December 4, 2007 0 => dom|291107020533 1 => |1196432585.jpg|Motion|ym,y,|30/11/07 That still looks correct to me.. Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-405997 Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 ok, hehe, well what I'm trying to do here is show you how to walk through an array, and verify along the way that you are getting expected results... so now, you want to explode the parts between |..? Let's look at the state of your array... you have 2 keys, so: $Data[0] and $Data[1] You want to explode 0 => dom|291107020533 and assign to $Data[0]...? That's going to create $Data[0][0] and $Data[0][1] Is that what you expected? Seems so... let's verify our next state of the variables: <?php foreach($Data as $Key => $Val){ $Data[$Key] = explode("|", $Val); } echo "<pre>"; print_r($Data[0]); die("</pre>"); Post results PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-406002 Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 We really need to pick up the pace here... it's 4:30 am here and I'm fading fast... would like to get this solved for you. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-406004 Share on other sites More sharing options...
Schlo_50 Posted December 4, 2007 Author Share Posted December 4, 2007 lol, sorry! Array ( [0] => dom [1] => 291107020533 ) Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-406005 Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 Ok, so still as expected... so next we are going to assign $loop_id to 291107020533 - correct? what is the state of $id? Let's do this: <?php foreach($file as $Key => $Val) { $Data[$Key] = explode("|", $Val); $loop_id = $Data[$Key][1]; var_dump($loop_id, $id); echo "<br>"; } PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-406007 Share on other sites More sharing options...
Schlo_50 Posted December 4, 2007 Author Share Posted December 4, 2007 Warning: Invalid argument supplied for foreach() in C:\apachefriends\xampp\htdocs\books1\area\display.php on line 23 Array ( [0] => dom|291107020533 [1] => |1196432585.jpg|Motion|ym,y,|30/11/07 ) is what i got so i changed your snippet from $file to $Data: foreach($Data as $Key => $Val) { $Data[$Key] = explode("|", $Val); $loop_id = $Data[$Key][1]; var_dump($loop_id, $id); echo "<br>"; } This then gave me: string(13) "291107020533 " int(2147483647) string(14) "1196432585.jpg" int(2147483647) Array ( [0] => Array ( [0] => dom [1] => 291107020533 ) [1] => Array ( [0] => [1] => 1196432585.jpg [2] => Motion [3] => ym,y, [4] => 30/11/07 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-406010 Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 Sorry, grabbed old code from up above... PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-406011 Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 Ok, well: loop_id: string(13) "291107020533 " id: int(2147483647) Notice that space at the end (after last 3) of loop_id? That's going to be a problem when we try to compare values... So, I think you can see now where you might need to adjust your code for the result you seek. Back to the drawing board... refer back to the walk-through as needed to stay focused on the state of your variables! Best of luck... PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-406014 Share on other sites More sharing options...
Schlo_50 Posted December 4, 2007 Author Share Posted December 4, 2007 Thanks man! I can solve that now! Quote Link to comment https://forums.phpfreaks.com/topic/80093-solved-passing-variable-problem/#findComment-406017 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.