Jump to content

[SOLVED] Passing Variable problem.


Schlo_50

Recommended Posts

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 ) ) 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.