Newb advice

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb advice

Post by MiniMonty »

Nice one.

Now to re-establish my super newb credentials I need to ask a basic html question.
The site I'm (slowly) building allows users to upload images - I want them to be able to choose
which gallery they upload to and I've reworked and copied my upload script so I now have one to send pics to
the Portraits directory, one for the Landscapes directory etc.,

So how do I code the font page (see picture) so that each radio button tells the submit button to run
the appropriate upload script? Or.... (please don't say it) do I need to use one script and pass a variable
to it from the radio button and send the pictures to the right place from there ?

best wishes
Monty
Image
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Newb advice

Post by jackpf »

Give each radio button a value of the gallery, then $_POST['thenameoftheradiobox'] will have the name of the gallery the chose. You might want to check the gallery they selected is valid too...
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Newb advice

Post by Weiry »

MiniMonty wrote:Nice one.

Now to re-establish my super newb credentials I need to ask a basic html question.
The site I'm (slowly) building allows users to upload images - I want them to be able to choose
which gallery they upload to and I've reworked and copied my upload script so I now have one to send pics to
the Portraits directory, one for the Landscapes directory etc.,

So how do I code the font page (see picture) so that each radio button tells the submit button to run
the appropriate upload script? Or.... (please don't say it) do I need to use one script and pass a variable
to it from the radio button and send the pictures to the right place from there ?

best wishes
Monty
Image
You would have something that looks like this as your HTML

Code: Select all

 
<form action='yourUploadPage.php' method='post' name='uploadForm'>
<input type="radio" name="galleryID" value="1" />Portraits <br/>
<input type="radio" name="galleryID" value="2" />Landscapes <br/>
<input type="radio" name="galleryID" value="3" />Travel <br/>
<input type='submit' name='uploadSub' value='Upload Image' />
</form>
 
And on your PHP page, you could use something like

Code: Select all

 
$galleryID = $_POST['galleryID'];
 
switch ($galleryID){
   case 1:
      //do your upload here for Portrait gallery
      //im just using an example query here for if you might store your galleryID to find the path later on
      $query = "INSERT INTO `yourTable` (`galleryID`, `field2`, ....) VALUES ('{$galleryID}', '{$value2}', .....)";
      break;
   case 2:
      //do your upload here for Landscape gallery
      break;
   case 3:
      //do your upload here for Travel gallery
      break;
   default:
      // return an error here
      break;
}
 
I haven't used a switch case with strings before so im just used to integers. You may just be able to check a string instead.
So you could change your radio button values to something like ( value='landscape' or value='portrait' ) and do the appropriate check in your switch case

I hope this helps ^^
cheers
Weiry
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb advice

Post by MiniMonty »

Great - thanks !

One strange problem...
My upload script will sometimes give an error
"You have exceeded the size limit!" but it will still allow the oversized
file to be uploaded - can't seem to see how to actualy stop the upload of the oversized
file.

Any ideas ?

Best wishes
Monty

Code: Select all

 
<?php
include 'uploads/doresize.php';
define ("MAX_SIZE","100");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if(isset($_POST['Submit']))
{
$image=$_FILES['image']['name'];
if ($image)
{
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
echo '<h1>Only try to upload .jpg, .jpeg, .png and .gif files!</h1>';
$errors=1;
}
else
{
$size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
$groovy = sizeof(glob("/Sites/com/shutterbugclub/www/images/gallery/abstracts/*"));
$groovy = ++$groovy;
print $groovy;
$image_name=$groovy.'.'.$extension;
$newname="gallery/abstracts/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}
if(isset($_POST['Submit']) && !$errors)
{
echo "<h1>File Uploaded Successfully!</h1>";
}
// NOW make the resize call!
img_resize ($_FILES [ 'image'] [ 'name'], $_FILES [ 'image'] [ 'tmp_name'], 537, $newname);
// Now make an array of the contents of the directory "portraits"
$array = glob('gallery/abstracts/*');
// write the .txt file with the new array
$myFile = "abstracts.txt";
$fh = fopen($myFile, 'w+') or die("can't open file");
$stringData = "arse=images/".implode(",images/",$array);
fwrite($fh, $stringData);
fclose($fh);
// APPEND the .txt fil with the total image number
$myFile = "abstracts.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = "&totalimgs=".$groovy;
fwrite($fh, $stringData);
fclose($fh);
?>
 
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Newb advice

Post by Weiry »

shouldn't:

Code: Select all

$size=filesize($_FILES['image']['tmp_name']);
be:

Code: Select all

$size=$_FILES['image']['size'];
??

Well at least in the code that I have it is at least.
Mine are defined as:

Code: Select all

$fileName   = $_FILES['imagefile']['name'];
$tmpName    = $_FILES['imagefile']['tmp_name'];
$fileSize   = $_FILES['imagefile']['size'];
$fileType   = $_FILES['imagefile']['type'];
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Newb advice

Post by jackpf »

You're still continuing with the upload even if an error occurs. You need to call die() or exit() after you display the errors to stop the script from parsing. Or put the whole upload process into a giant "if" statement.
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb advice

Post by MiniMonty »

jackpf wrote:You're still continuing with the upload even if an error occurs. You need to call die() or exit() after you display the errors to stop the script from parsing. Or put the whole upload process into a giant "if" statement.
That works ! :D

OK, this whole thing is really coming on nicely now and it's time to start some 'real world' testing of the rest of the site.
I spoke to my host and we have mysql and phpmyadmin installed - but - he heavily suggested we go for joomla
or wordpress - what do people think ?

Also, I'd like to have a play around with phpmyadmin but can't seem to find how to open it / log into it on the server.
Where do I look ?

Best wishes
Monty

EDIT - found it !
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb advice

Post by MiniMonty »

Making tables...

I'm following a tutorial and at step 4 I'm supposed to run this script (below) to create a table.
I get this error:
Parse error: syntax error, unexpected '{' in /Sites/com/shutterbugclub/www/create_Table.php on line 25
(26 as it appears below) but can't make sense of it.

Also I'm getting TONS of syntax errors because I'm using simpleText to write (and paste) code and it very often includes invisibles and white space which takes ages to find and delete - does anyone know of a decent (free) editor I can use on a Mac ?

Best wishes
Monty

Code: Select all

 
<?php
include_once"connect_to_mysql.php";
print"Success in database file CONNECTION";
$result="CREATE TABLE myMembers(
id int(11) NOT NULL auto_increment,
firstname varchar(255) NOT NULL,
lastname varchar(255) NOT NULL,
address1 varchar(255) NOT NULL,
address2 varchar(255) NOT NULL,
state varchar(255) NOT NULL,
city varchar(255) NOT NULL,
zip varchar(255) NOT NULL,
phone varchar(255) NOT NULL,
bio_body text NOT NULL,
email varchar(255) NOT NULL,
password varchar(255) NOT NULL,
sign_up_date date NOT NULL default '0000-00-00',
last_log_date date NOT NULL default '0000-00-00',
account_type varchar(255) NOT NULL default 'client_type_1',
account_notes varchar(255) NOT NULL,
email_activated enum('0','1') NOT NULL default '0',
PRIMARY KEY (id),
UNIQUE KEY email (email)
) ";
if (mysql_query($result)){echo "Success in TABLE creation!......<br/><br/><b>hat completes the table setup, now delete the file <br/>
named 'create_Table.php' and you are ready to move on. Let us go!</b>";} else {echo "no TABLE created. You have problems in the system already, backtrack and debug!";
}
exit();
?>
 
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Newb advice

Post by jackpf »

I don't get any syntax errors :/
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb advice

Post by MiniMonty »

Finally got it to run by reformatting the last few lines (about a hundred times).

Code: Select all

 
if(mysql_query($result)){
echo"Success";
}else{
echo"no TABLE created. You have problems in the system already, backtrack and debug";
}
exit();
?>
 
And when it finally ran it came back "no TABLE created" !
LOL (cynically) and Grrrrrrrrr....
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Newb advice

Post by jackpf »

Check what's in mysql_error()...
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb advice

Post by MiniMonty »

I got fed up and made the table via myphpadmin. (which I need to learn anyway).

I tell you what, as a front end Flash developer (designer really) I'm really glad to finally be getting my
hands dirty with the back end. Although I'm finding it "challenging", it's a lot of fun and actually not that
difficult to learn the basics. php is a LOT more forgiving than actionscript and once I get the Flash end front talking properly
to the php / mysql back end I see a great future for fin and funky web apps that can be beautiful and fun to look at while
doing cool and groovy things.

Thanks for all your help so far - (but don't abandon me out in the woods on my own just yet) !

Best wishes
Monty
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Newb advice

Post by jackpf »

Hmm...I have to say, I'm not so fond of flash myself. I'm more of a javascript person :D

But yeah, PHP is cool.
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb advice

Post by MiniMonty »

Hi all,

so fifteen days without having to ask questions ! Yeeeaaah !!
And now I'm stuck again...
As you know by now I'm trying to build a membership system - if you feel like joining up it's free and it's here:
[url]http://%20www.shutterbugclub.com[/url]
it's going OK but now I'm stumbling over the member profile page. Code below. The page displays OK, shows the default member picture (an avatar) and the member info. But I get these errors: (line numbers plus one on code below).
Notice: Undefined index: id in /Sites/com/shutterbugclub/www/profile.php on line 25
Notice: Undefined index: bio_body in /Sites/com/shutterbugclub/www/profile.php on line 52
Notice: Undefined index: blab_field in /Sites/com/shutterbugclub/www/profile.php on line 78
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /Sites/com/shutterbugclub/www/profile.php on line 134

All and any help / advice much appreciated.
Best wishes
Monty

Code: Select all

 
<?php 
session_start(); 
 
// Connect to database
//include_once "scripts/connect_to_mysql.php";
 $host = "localhost";
     $user="blahblahblah";
     $password="blahblahblah";
     $database="blahblahblah";
     $connection = mysql_connect($host, $user,$password)
               or die ("Couldn't connect to server.");
     $db = mysql_select_db($database, $connection)
               or die ("Couldn't select the database.");
// Now let's initialize vars to be printed to page in the HTML section so our script does not return errors 
// they must be initialized in some server environments
$id = "";
$firstname = "";
$lastname = "";
$bio_body = "";
$website = "";
$youtube = "";
$user_pic = "";
$blabberDisplayList = "";
// If coming from category page
if ($_GET['id']) {
    
     $id = $_GET['id'];
 
} else if (isset($_SESSION['id'])) {
    
     $id = $_SESSION['id'];
 
} else {
    
   include_once "index.php";
   exit();
}
$id = mysql_real_escape_string($id);
$id = eregi_replace("`", "", $id);
$sql = mysql_query("SELECT * FROM myMembers WHERE id='$id'");
 
while($row = mysql_fetch_array($sql)){ 
 
    $firstname = $row["firstname"];
    $lastname = $row["lastname"];   
    $email = $row["email"];
    //$email = "<a href=\"mailto:$email\"><u><font color=\"#006600\">Mail</font></u></a>";  
    $sign_up_date = $row["sign_up_date"];
    $sign_up_date = strftime("%b %d, %Y", strtotime($sign_up_date));
    $last_log_date = $row["last_log_date"];
    $last_log_date = strftime("%b %d, %Y", strtotime($last_log_date));  
    $bio_body = $row["bio_body"];   
    $website = $row["website"];
    $youtube = $row["youtube"];
    ///////  Mechanism to Display Pic. See if they have uploaded a pic or not  //////////////////////////
    $check_pic = "members/$id/image01.jpg";
    $default_pic = "members/0/image01.jpg";
    if (file_exists($check_pic)) {
    $user_pic = "<img src=\"$check_pic\" width=\"200px\" />"; // forces picture to be 100px wide and no more
    } else {
    $user_pic = "<img src=\"$default_pic\" width=\"200px\" />"; // forces default picture to be 100px wide and no more
    }
    ///////  Mechanism to Display Youtube Channel or not  //////////////////////////
    if ($youtube == "") {
    $youtubeChannel = "<br />This user has no YouTube channel yet.";
    } else {
    $youtubeChannel = ' <script src="http://www.gmodules.com/ig/ifr?url=http://www.google.com/ig/modules/youtube.xml&up_channel=' . $youtube . '&synd=open&w=290&h=370&title=&border=%23ffffff%7C3px%2C1px+solid+%23999999&output=js"></script>  '; // forces default picture to be 100px wide and no more
    }   
 
} // close while loop
 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$style_sheet = "default";
?>
<?php
// Place Blab post into database
$blab_outout_msg = "";
if ($_POST['blab_field'] != ""){
    
     // Delete any blabs over 20 for this member
     $sqlDeleteBlabs = mysql_query("SELECT * FROM blabbing WHERE mem_id='$id' ORDER BY blab_date DESC LIMIT 50");
     
     $bi = 1;
     
     while ($row = mysql_fetch_array($sqlDeleteBlabs)) {
         
         $blad_id = $row["id"];
         if ($bi > 20) {
             
              $deleteBlabs = mysql_query("DELETE FROM blabbing WHERE id='$blad_id'");
         }
         $bi++;
     }
     // End Delete any blabs over 20 for this member
    
     $blab_field = $_POST['blab_field'];
     $blab_field = stripslashes($blab_field);
     $blab_field = strip_tags($blab_field);
     $blab_field = mysql_real_escape_string($blab_field);
     $blab_field = eregi_replace("'", "'", $blab_field);
     
     $sql = mysql_query("INSERT INTO blabbing (mem_id, the_blab, blab_date) 
     VALUES('$id','$blab_field', now())")  
     or die (mysql_error());
     
     $blab_outout_msg = "Your Blab has been posted!";
}
 
?>
 
 
 
 
<?php
$the_blab_form = "";
if (isset($_SESSION['id'])) {
    
    if ($_SESSION['id'] == $id){
     $the_blab_form = '
     ' . $blab_outout_msg . '
 
<form action="profile.php" method="post" enctype="multipart/form-data" name="blab_from">
<textarea name="blab_field" rows="3" style="width:97%;"></textarea>
Blab away  (220 char max) <input name="submit" type="submit" value="submit" />
</form>';
    }
}
 
?>
 
<?php
$sql_blabs = mysql_query("SELECT id, mem_id, the_blab, blab_date FROM blabbing WHERE mem_id='$id' ORDER BY blab_date DESC LIMIT 20");
 
while($row = mysql_fetch_array($sql_blabs)){
    
    $blabid = $row["id"];
    $uid = $row["mem_id"];
    $the_blab = $row["the_blab"];
    $blab_date = $row["blab_date"];
    $blab_date = strftime("%b %d, %Y, %Y %I:%M:%S %p", strtotime($blab_date));
    
                $blabberDisplayList .= '
                    <table width="100%" align="center" cellpadding="4" bgcolor="#A6D2FF">
        <tr>
          <td width="93%" bgcolor="#D9ECFF"><span style="font-size:10px; font-weight:bold; color:#A6A6A6;">' . $blab_date . '</span><br />
            ' . $the_blab . '</td>
        </tr>
      </table>';
    
}
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="Description" content="Profile for <?php print "$firstname $lastname"; ?>" />
<meta name="Keywords" content="<?php print "$firstname, $lastname"; ?>" />
<meta name="rating" content="General" />
<meta name="ROBOTS" content="All" />
<title>Site Profile for <?php print "$firstname $lastname"; ?></title>
<link href="style_profiles/<?php print "$style_sheet"; ?>.css" rel="stylesheet" type="text/css" />
<link rel="icon" href="http://www.yourdomain.com/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="http://www.yourdomain.com/favicon.ico" type="image/x-icon" />
</head>
 
<body>
<?php include_once "header_template.php"; ?>
<table width="950" align="center">
  <tr>
    <td width="758"><br />
      <table width="90%" border="0" align="center" cellpadding="6">
      <tr>
        <td width="48%" valign="top">
        <?php print "$firstname $lastname"; ?>
        <br />
        <?php print "$user_pic"; ?>
        <br />
        Member Since: <?php print "$sign_up_date"; ?>
        <br />
       <?php print "$youtubeChannel"; ?>
       </td>
        <td width="52%" valign="top">
        <br />
        <strong><u><?php print "$firstname $lastname"; ?>'s Blabs:</u></strong>
        <br />
        <?php print "$the_blab_form"; ?>
        <div style="width:100%; height:180px; overflow:auto; overflow-x:hidden;">
        <?php print "$blabberDisplayList"; ?>
        </div>
        <br />
        <br />
        <strong><u><?php print "$firstname $lastname"; ?>'s Location Details:</u></strong>
        <br />
        <br />
        <strong><u>About <?php print "$firstname $lastname"; ?></u></strong>
        <br />
        <?php print "$bio_body"; ?>
        <br />
 
        
        </td>
      </tr>
      <tr>
        <td colspan="2" valign="top">&nbsp;</td>
        </tr>
      </table>
      <p><br />
        <br />
    </p>
      <p>&nbsp;</p>
      <p><br />
        <br />
        <br />
        <br />
        <br />
      </p></td>
    <td width="180" valign="top"><?php include_once "right_AD_template.php"; ?></td>
  </tr>
</table>
<?php include_once "footer_template.php"; ?>
</body>
</html>
 
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Newb advice

Post by jackpf »

Use isset().
Post Reply