Shoutbox

php coding help - 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 coding help (/showthread.php?tid=30977)

php coding help by Eljay on 09-04-2004 at 05:26 PM

im in the middle of coding my website (GameSmart)
and im doing the reviews section so i need to know how to include smilies
what i mean is like if somebody types :P then in the review it shows :P
how is this done?


RE: how to include smilies by fluffy_lobster on 09-04-2004 at 05:59 PM

Before it is outputted, you need to pass it through str_replace:

code:
$search = array(':P',':)');
$replace = array('<img src="img/tongue.gif">', '<img src="img/smile.gif">');
echo str_replace($search, $replace, $post):


That's an example of using it... customise it for yourself.  You could also add the image code to each as you replace it in by doing a foreach loop
RE: php coding help by Eljay on 09-04-2004 at 06:21 PM

ok thanx fluffy_lobster
now i need more help but not with smilies
i have a reviews section now with a couple of test reviews
but i want to load the review into an iframe using the id variable from the mysql db
heres the code i have:

code:
<?
$username="*****";
$password="*****";
$database="*****";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM reviews";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Review List</center></b><br><br>";
$i=0;
while ($i < $num) {

$id=mysql_result($result,$i,"id");
$Title=mysql_result($result,$i,"Title");
$Platform=mysql_result($result,$i,"Platform");
$Review=mysql_result($result,$i,"Review");
$Rating=mysql_result($result,$i,"Rating");
$Reviewer=mysql_result($result,$i,"Reviewer");
$Date=mysql_result($result,$i,"Date");

echo "
<table border=0 colspan=2>
<tr>
<td>
<a href="link to review" target="iframe">$Title</a>
</td>
<td><iframe goes here>
</tr>";

$i++;
}

?>


RE: php coding help by -dt- on 09-05-2004 at 05:45 AM

you dont have to use echo to print html you can just close that part of the script with " ?>" and then writing all the html you want then using "<?" to start the php again then whenever you need to print a varible or something into the html just use <?echo "$whatever";?>
eg

code:
<?
$username="*****";
$password="*****";
$database="*****";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM reviews";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Review List</center></b><br><br>";
$i=0;
while ($i < $num) {

$id=mysql_result($result,$i,"id");
$Title=mysql_result($result,$i,"Title");
$Platform=mysql_result($result,$i,"Platform");
$Review=mysql_result($result,$i,"Review");
$Rating=mysql_result($result,$i,"Rating");
$Reviewer=mysql_result($result,$i,"Reviewer");
$Date=mysql_result($result,$i,"Date");
?>
<table border=0 colspan=2>
<tr>
<td>
<a href="link to review" target="iframe"><?echo "$Title";?></a>
</td>
<td><iframe goes here>
</tr>
<?
$i++;
}

?>

sorry if thats a little offtopic just thought he might like to know that.
and i dont quite get what you need help with
RE: php coding help by k776 on 09-05-2004 at 06:09 AM

Using the way -dt- said, change the table to:

code:
<table border=0 colspan=2>
<tr>
<td>
<a href="<? echo "$Review";?>" target="iframe"><?echo "$Title";?></a>
</td>
<td><iframe name="iframe"></iframe>
</tr>
</table>
should do it. That what you looking for??

Edit: All you php know-alls out there. Fluffy lob gave the code (2nd message). I turned it into this:
code:
<?
$search = array('0star','1star','2star','3star','4star','5star');
$replace = array('Rating Coming Soon','<img src="images/download/star.gif">','<img src="images/download/star.gif"><img src="images/download/star.gif">','<img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif">','<img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif">','<img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif">');
echo str_replace($search, $replace, $post);
?>
but it doesn't work, any idea why??
RE: php coding help by Jammie on 09-05-2004 at 08:15 AM

code:
<?
$search = array('0star','1star','2star','3star','4star','5star');
$replace = array('Rating Coming Soon','<img src="images/download/star.gif">','<img src="images/download/star.gif"><img src="images/download/star.gif">','<img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif">','<img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif">','<img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif">');
echo str_replace($search, $replace, $post);
?>


This code would output as blank as there is no value being past to the function within the $post variable. To make the code work you would need to use something along the lines of the following:

code:
<?
$post = "0star 1star";
$search = array('0star','1star','2star','3star','4star','5star');
$replace = array('Rating Coming Soon','<img src="images/download/star.gif">','<img src="images/download/star.gif"><img src="images/download/star.gif">','<img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif">','<img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif">','<img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif"><img src="images/download/star.gif">');
echo str_replace($search, $replace, $post);
?>


Hope this helps :D

Jamie
RE: php coding help by fluffy_lobster on 09-05-2004 at 04:35 PM

First of all, if you want to use an iframe, you need an actual page which can be loaded into the iframe.  The simplest way to do this is to make a page which fetches the review content of any of the reviews.

As per how to echo content, when you're incorporating variables into the string i much prefer lee's original method.  Popping in and out of php mode is both messy and probably less efficient.

Also, mysql_result should only be used if you want a specific field.  in your case, as you're using them all you can use mysql_fetch_array.  This function returns false when you run out of rows, which saves you doing the count too.

Anyway... here is my revised version of the code.  Because of the iframe it's split into two pages:

first of all the original page:

code:
<?
$username="*****";
$password="*****";
$database="*****";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM reviews";
$result=mysql_query($query);
mysql_close();
?>
<b><center>Review List</center></b><br><br>

<table border=0>
<tr><td>
<table border=0>
<?
while ($row = mysql_fetch_array($result)) {

echo '
<tr>
<td>
<a href="iframe.php?id=' . $row['id'] . '" target="iframe">' . $row['Title'] . '</a>
</td></tr>';
}

?>
</table>
</td>
<td>
<iframe name="iframe" src="about:blank" width="400" height="300"></iframe>
</td>
</tr>
</table>


The html layout you started seemed a bit confusing.  I've redone it the way I think you mean... change it back if it was meant to be different in the context of a whole page.

Then you have iframe.php which goes a little bit like this:

code:
<?
$username="*****";
$password="*****";
$database="*****";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT Review FROM reviews WHERE id = $_GET[id]";
$result=mysql_query($query);
mysql_close();
echo mysql_result($result, 0);
?>



RE: php coding help by k776 on 09-06-2004 at 09:18 AM

How do you write data/text to a database??

Im using my own form, set with method="post" action="scripts/add.php". What do I add in scripts/add.php??

Edit: I want to add the following feilds:

catid,
mplink,
name,
dllink,
desc,
rate,
type,
pic

to a table called download_items.


RE: php coding help by WDZ on 09-06-2004 at 09:33 AM

quote:
Originally posted by k776
How do you write files to a database??

Im using my own form, set with method="post" action="scripts/add.php". What do I add in scripts/add.php??
http://www.php.net/manual/en/features.file-upload.php
http://p2p.wrox.com/topic.asp?TOPIC_ID=11883

Those links should point you in the right direction... :p
RE: php coding help by k776 on 09-08-2004 at 05:30 AM

Sorry WDZ, I meant to say infomation/text, not actual files :$

Also, what is a good php password code so stop visitors going into this page im making to put things into the database?? Maybe storing the username and password I choose in the database then asking for it?? Any help would be nice.


RE: php coding help by WDZ on 09-08-2004 at 12:51 PM

quote:
Originally posted by k776
Sorry WDZ, I meant to say infomation/text, not actual files :$
Well, it shouldn't be too hard. First you might want to do some validation of the values that were submitted, though that's not as important if only trusted people can submit.

code:
if(!$_POST['name']) {
    die("You must enter a name!");
}

You could also check to make sure the length isn't too long and stuff like that. Next you should make sure the data is ready to be used in a query by applying addslashes()...

$name = addslashes($_POST['name']);

However, this step might not be needed if your server has magic quotes enabled, because that will automatically add slashes. Running the function again will result in doubled slashes, which is annoying. :dodgy:

Now to actually insert the data...

mysql_query("INSERT INTO tablename (name) VALUES ('$name')");

That's an example of inserting just one field. To insert all the fields you listed, you'd do something like this...

mysql_query("INSERT INTO tablename (catid,mplink,name,dllink,desc,rate,type,pic) VALUES ('$catid', '$mplink', '$name', '$dllink', '$desc', '$rate', '$type', '$pic')");

I don't know what all your fields are for, so I can't give a perfect example. Also, you'd have to call your mysql_connect() and mysql_select_db() before mysql_query(), of course.

Google for some tutorials... I bet there's tons of them...
RE: php coding help by k776 on 09-09-2004 at 12:10 AM

I took what you said, edited it a tiny bit.

Im using my own form, set with

code:
method="post" action="<? echo $PHP_SELF; ?>"
with
code:
<?
require ("scripts/add.php");
?>
at the top of the page. Then in scripts/add.php, I have this:
code:
<?
require_once ("scripts/config.php");

$errors = array();
if ($_SERVER['REQUEST_METHOD']=="POST"){
$errors = process_form($errors);
}
function process_form($errors) {

$catid = $_REQUEST['catid'] ;
$mplink = $_REQUEST['mplink'] ;
$name = $_REQUEST['name'] ;
$dllink = $_REQUEST['dllink'] ;
$desc = $_REQUEST['desc'] ;
$pic = $_REQUEST['pic'] ;
   
if (empty($catid)) {
    $errors["No catid"]="Empty catid";
    }
if (empty($mplink)) {
    $errors["No mplink"]="Empty mplink";
    }
if (empty($name)) {
    $errors["No name"]="Empty name";
    }
if (empty($dllink)) {
    $errors["No dllink"]="Empty dllink";
    }
if (empty($desc)) {
    $errors["No desc"]="Empty desc";
    }
if (empty($pic)) {
    $errors["No pic"]="Empty pic";
    }
if (count($errors) == 0) {

mysql_query("INSERT INTO download_items (catid,mplink,name,dllink,desc,rate,type,pic) VALUES ('$catid','$mplink','$name','$dllink','$desc','$rate','$type,'$pic')");
header( "Location: scripts/add2.htm" );

}
return $errors;
}
function print_error($error) {
  echo "<b><font color=red>Error:</b> $error</font><br /><br />";
}
?>

but it doens't work. No errors, it does exactly what I want it to (check for missing fields, if none process, if some show error on the form). It just doesn't insert into the database, I have no idea why. Any ideas?? (rate and type dont need to be checked for missing feilds cause they are set to "Not Available by default".

(config hold the username, password and database name.) The code in it is:
code:
<?
$username ="******";
$password ="******";
$database="******";
$connection = mysql_connect("localhost","$username","$password");
@mysql_select_db("$database", $connection) or die( "<br><center><h2>Unable to select database, please refresh.</h2>If problem persists, please contact me using the form and report the error you are receiving if any.</center>");
?>

As for the slashes, how can I tell if I have magic quotes enabled??

So, if you find what is wrong, can you submit 2 versions, one with and one without slahes command thing, just in case.

As for what its for, adding downloads to my site.
catid = I have 8 different catagorys so catid is somewhere between 1-8
mplink = Main page link
name = Name of the download
dllink = Download link
desc = Description of the download
rate = My rating for the download
type = The type of download it is, Freeware, Shareware etc.
pic = A picture of the download

k776
RE: php coding help by WDZ on 09-09-2004 at 02:56 AM

Hmm... I don't see where print_error() is called. Maybe one of the fields is missing for some reason but you don't know about it because the error is never echoed.

You should do something like the following...

code:
foreach($errors as $error) {
    print_error($error);
}

quote:
Originally posted by k776
As for the slashes, how can I tell if I have magic quotes enabled??
http://php.net/get_magic_quotes_gpc

There's some nice examples there that show how to detect and handle magic quotes.
RE: php coding help by k776 on 09-09-2004 at 03:48 AM

ok, print_error() is on the form in this code:

code:
<? if (array_key_exists("No catid",$errors)) print_error($errors["No catid"]); ?>
One for each part of the form I want to check. It displays it on the form for me.

Next, Thanks for the link, checking now.............. dont understand. How can I check if magic quotes is enabled?? Can you use the code in add.php to give me an example??
RE: php coding help by WDZ on 09-09-2004 at 04:23 AM

quote:
Originally posted by k776
ok, print_error() is on the form in this code:
code:
<? if (array_key_exists("No catid",$errors)) print_error($errors["No catid"]); ?>
One for each part of the form I want to check. It displays it on the form for me.
Oh, OK. Well, I'm stumped. :p I can't spot any obvious problems in your code.

Do the errors work properly? If you leave a field blank and submit the form, you see the error? Is the inserting into the database the thing that doesn't work? If so, then the problem might be that count($errors) is returning something other than 0. Maybe you should do a foreach($errors) just for debugging, and see what's making it not 0.

quote:
Next, Thanks for the link, checking now.............. dont understand. How can I check if magic quotes is enabled?? Can you use the code in add.php to give me an example??
OK... I modified the code on php.net to ADD slashes if magic quotes is disabled. It will make sure all the values in $_REQUEST are ready to be used in the MySQL query.

code:
if (!get_magic_quotes_gpc()) {
    function addslashes_deep($value) {
        $value = is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value);
        return $value;
    }
    $_REQUEST = array_map('addslashes_deep', $_REQUEST);
}

$catid = $_REQUEST['catid'];
// etc...

RE: php coding help by k776 on 09-09-2004 at 04:51 AM

ok, yes, the errors do work. If I leave it blank, it does show the error. As for the foreach($errors) part, where do I put that??

added the code you gave me, uploading now................. still doesn't work, but I'll keep it there anyway.

*Chris Boulton suggested

code:
mysql_query("INSERT INTO download_items (catid,mplink,name,dllink,desc,rate,type,pic) VALUES ('$catid','$mplink','$name','$dllink','$desc','$rate','$type,'$pic')") or die(mysql_error());
Im gonna try that now.*

A-ha, now we have somethign to work on....... *Thank Chris*
quote:
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc,rate,type,pic) VALUES ('2','test','test','test','test','',

RE: php coding help by WDZ on 09-09-2004 at 05:20 AM

Ah, OK...

code:
mysql_query("INSERT INTO download_items (`catid`,`mplink`,`name`,`dllink`,`desc`,`rate`,`type`,`pic`) VALUES ('$catid','$mplink','$name','$dllink','$desc','$rate','$type,'$pic')") or die(mysql_error());

Try that... MySQL doesn't like the "desc" because it's a reserved word.
RE: php coding help by k776 on 09-09-2004 at 05:42 AM

ok, I used your suggestion and Chris Boultons ('$type, to '$type', (extra ')) and now it works. Thanks WDZ.

4 new questions:

code:
<option value="0" <? if ($_REQUEST['rate'] == "0") echo "checked"; ?>>0 - Not Yet Rated</option>
That code is suppose to remember the option if I forget a feild and it resets the page but it doens't. I got text feilds to work, but not select feilds. Any suggestions.

I want a password code so only I can enter the "Add download" page. Please can someone lead me to the right site or give me a code here that can do just that??

I want it to echo each part of the script, eg:
catid added.....
mplink added.....
name added.....
etc etc

And finally, I want it to redirect me back to the add download page.

Please help.
RE: php coding help by WDZ on 09-09-2004 at 12:38 PM

quote:
Originally posted by k776
('$type, to '$type', (extra '))
Doh... how could I have missed that? My bug-spotting skills are getting rusty... :p

So there were 2 syntax errors in the query... glad to hear it's working now.

quote:
Originally posted by k776
That code is suppose to remember the option if I forget a feild and it resets the page but it doens't. I got text feilds to work, but not select feilds. Any suggestions.
"checked" is for checkboxes and radio buttons. For <select> menus, use "selected" instead.
RE: php coding help by k776 on 09-09-2004 at 08:46 PM

Thanks WDZ. Its working now :)

New questions:

1. Is this how to put a [Delete] button next to name so it deletes the table I want without having to go into mySQL??

code:
$sql = "DELETE FROM `download_items` WHERE id = '$id' LIMIT 1";
echo "[Delete]<br>\n";

2. I also want a [Edit] button that pulls the info from the download, onto a form in the right places, and allows me to edit it. How can that be done?? (just like this baord, when you click edit, it pulls the name of the thread description etc. If it is too hard, Ill just have to edit it in mySQL :( )

3. I have game and program downloads. I want to have a select box that has to be chosen first to open up the other selections. eg:  Select1 and Select2.
Select one asks for Game Download or Item downloads. If items is chosen, it displays the catagories in select2, if game is chosen, differnet ones. See what I mean?? Currently, only 1 is possible, I dont want to have to make two forms. So until Select1 is chosen, can select 2 be greyed out somehow?? Next, I need the coding. Most probably an if statment, like
code:
if "select == item {
code to process form here into the right place (got this one)
} else {
the code to put it in another place (got this too)
}

Thanks for the help so far everyone.
RE: php coding help by fluffy_lobster on 09-10-2004 at 06:19 PM

Listen dude, reading those last questions, I don't think you've really grasped how php, forms and mysql interact with each other.  I suggest you read the unrivalled Webmonkey PHP/MySQL Tutorial.  That covers all the basic insert/update/delete/select functions that a db system like yours wants to employ.


RE: php coding help by k776 on 09-10-2004 at 09:37 PM

see, Ive never been good at going to site and searching for the infomation (I got lost at Webmonkey), so would really appreciate it if either you or WDZ could give me direct links to my problem or help me here please.


RE: php coding help by WDZ on 09-11-2004 at 03:08 AM

On lobster's link, all you have to do is read and keep clicking "next lesson" or whatever... :p

It's probably not a bad idea to study the basics before diving into specifics... :^)


RE: php coding help by Eljay on 09-11-2004 at 06:38 AM

heres where i learnt the basics
http://www.freewebmasterhelp.com/tutorials/phpmysql


RE: php coding help by k776 on 09-13-2004 at 09:37 AM

fluffy_lobster, thanks for the link, it got me started :) Thanks for the help WDZ. leejeffery, thanks for the link, helped me too.

And thanks to another friend, the myBB boards, and your help, its all fixed.

Other things I had to do:
I added a hidden input field,
put a code to request the id on the edit.php page
changed index to 'index' cause it was a reserved word.
Along with more syntax errors.

I have also fixed other parts of my site with a friend. Its all fixed now. So thanks for everyones help.


RE: php coding help by Eljay on 09-13-2004 at 04:32 PM

i need help again
i have my reviews page sorted now and im just formatting it to make it look better
i need a way of limiting the width of the text in the iframe im using because at the moment if a review is longer than 55 letters it makes it have a scrollbar at the bottom and if a review is realy long it will just be on one line which is annoying
another problem is when i tried limiting letters it would cut off half way through a word so is there any way of limiting amount of words on a line?