Shoutbox

PHP help please - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP help please (/showthread.php?tid=47141)

PHP help please by Ezra on 07-03-2005 at 04:25 PM

I'm kinda new to making my own functions and i'm having some trouble...

I made this function in a php script I included

code:
function query_db($table){
$data = mysql_fetch_array(mysql_query("SELECT * FROM `".$table."` WHERE id=".$_GET['id']));
return $data;
}


and then I wanted to execute it, and typed

code:
query_db("imdb_data_title");


after the including, but that didn't work, later a friend helped a little and we managed to make it work like this:

code:
$data = query_db("imdb_data_title");


If somone knows a better way of doing that please tell me :)

Then another thing that doesn't work yet...

I have another function in the same script

code:
function link_rewrite(){
//Voor op de profielen worden de films veranderd door links
//actor
$actor = str_replace("Paris Death Monkeys (2004)","<a href=\"title.php?id=2\">Paris Death Monkeys (2004)</a>", $actor);
$actor = str_replace("Ameland: The Movie (2003)","<a href=\"title.php?id=1\">Ameland: The Movie (2003)</a>", $actor);
$actor = str_replace("\"Bredero\" (2004)","<a href=\"title.php?id=0\">\"Bredero\" (2004) </a>", $actor);

//writer
$writer = str_replace("Paris Death Monkeys (2004)", "<a href=\"title.php?id=2\">Paris Death Monkeys (2004)</a>", $writer);
$writer = str_replace("Ameland: The Movie (2003)", "<a href=\"title.php?id=1\">Ameland: The Movie (2003)</a>", $writer);
$writer = str_replace("\"Bredero\" (2004)", "<a href=\"title.php?id=2\">\"Bredero\" (2004)</a>", $writer);
}


It's a little longer but they are all str_replaces with other names
then when I wanted to execute the function I typed this

code:
link_rewrite();


But it doesn't work :(

Can someone please help me with this??

TIA...


RE: PHP help please by Eljay on 07-03-2005 at 04:31 PM

for both of them use global variables

e.g for the first one

add
global $data;
at the beginning of the function
and it will store the mysql data retrieved in the var and it will be usable outside of the function (also you then dont need the return line)

for the second one same thing applies, use global $actor; and global $writer;


RE: PHP help please by Ezra on 07-03-2005 at 04:39 PM

brilliant :), it works :D

Still have to change a lot to make it work better, but you helped me BIG :-)

---------------------------------------------------------------------------------------------------

:o srry for the double post

a little question about global. I tried to make $data['cast'] a global but it said unexpected "[", can't I use this way to make a global, or should i just use $data ?


RE: PHP help please by Dempsey on 07-03-2005 at 06:02 PM

just use $data as its an array and its all contents will be globally available then


RE: PHP help please by Fourjays on 07-03-2005 at 10:36 PM

If your new to php (like myself), you can find loads of tutorials. I found this one particularly good:

http://www.php-mysql-tutorial.com/

it seems to cover almost all the basics. I used another tutorial before this, but I wish I ahd found this one earlier - it is so much more detailed, as the author has explained what is going on, rather than just saying "do this but dont do that".


RE: PHP help please by jren207 on 07-04-2005 at 12:09 AM

I was bored and made this little function. It only really works for getting info from one row of a table that you specify the id or whatever from the $_GET['uname'] part. (I was testing it on a users table). You specifiy the table name and the field you want, db_q('tablename', 'fieldname'); when you use the function

i'm sorta newb, blah :P

code:
<?php

// db function
function db_q($table, $field){

// get variable
$get_name = $_GET['uname'];

// db query
$db_query = mysql_query("SELECT * FROM $table WHERE id = $get_name");

// db fetch array
while($query_row = mysql_fetch_array($db_query)){

// query result
$query_result = $query_row[$field];
}

// return result
return $query_result;
}

// using the function

// echo html table part
echo('<table border="1">
<tr>
<td>
');
// run function
$display_result = db_q('users', 'username');
// echo result from function returned
echo('' . $display_result . '');
// echo html table bit
echo('
</td>
<td>
');

// run function with another table field used
$display_result = db_q('users', 'website');

// echo this field's result
echo('' . $display_result . '');

// echo end of html table
echo('
</td>
</tr>
</table>
');
?>


RE: PHP help please by Ezra on 07-04-2005 at 01:44 PM

I used it to make a page like IMDB, but just for some films I made with a friend. It's not done yet, but works very well now :)

BTW... I ripped the lay-out from IMDB couple of years ago already so it doesn't look exaclty the same anymore, but it's just made for fun and to learn PHP better :)

http://school.tsdme.nl/IMDB/title.php?id=2

PS. This site is made for the only purpose of fun for me and a couple of friends, not for phising or anything like that...

I made 3 pages, title for the movies, profile for the profiles of the people and mod.inc.php with functions like opening my database, that dynamic query and a link adder, that basicly changes strings to links.