Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof

eHacked posted:

That seems way too simple.

It's about that simple. I'd add a few more headers to make sure IE doesn't choke on it, among other things:

php:
<?
header('Pragma: public');
header('Cache-Control: max-age=0');
header('Content-Type: text/plain');
header('Content-Length: ' . strlen($fl));
header('Content-Disposition: attachment; filename="export.txt"');
echo $fl;
exit;
?>

Adbot
ADBOT LOVES YOU

wil
Mar 2, 2009

Eararaldor posted:

Funny you should mention that. I was actually checking it out but was a little miffed as to how I actually implement it.

Download jquery.ui from http://jqueryui.com/download

The following code will build up on the previous "tutorial" I posted a few days ago.

(So if you want the form to submit to a database use that code, this is merely demonstrating the date picker being submitted to a form and response via ajax.)

Instead of spamming the board, I've posted a code example: http://littlesparrows.com/sa/datepicker/ - view source to see what's going on.

I've rared it for you here so you can download the source:

http://littlesparrows.com/sa/datepicker.rar

Hope it helps :)

wil fucked around with this message at 19:03 on Mar 12, 2009

Eararaldor
Jul 30, 2007
Fanboys, ruining gaming since the 1980's

wil posted:

Download jquery.ui from http://jqueryui.com/download

The following code will build up on the previous "tutorial" I posted a few days ago.

(So if you want the form to submit to a database use that code, this is merely demonstrating the date picker being submitted to a form and response via ajax.)

Instead of spamming the board, I've posted a code example: http://littlesparrows.com/sa/datepicker/ - view source to see what's going on.

I've rared it for you here so you can download the source:

http://littlesparrows.com/sa/datepicker.rar

Hope it helps :)

Very much so!

I reall apreciate the help, however I have one more question left to ask.
How would I go about converting 00/00/00 to sql datetime?

I guess I could cheat and swap the output to a varchar instead, but would be interested to hear how the proffesionals do it.

wil
Mar 2, 2009
It can be formatted using the formatDate parameter on the javascript side, but it might be faster to do it on the server side in the processForm.php file.



Change your processForm.php to reflect this:

code:
<?php


$date = $_POST['datepicker']; //this is the field we set on form, and it is being populated by ajax when the form is submit.

$mySqlDateTime= date( "Y-m-d H:i:s", strtotime( $date ) );

if($date != null){
	echo "You have submited the form. The date you submitted was: " . $mySqlDateTime;

} else {
	echo "Hey dummy, you forgot to put in a date.";
}


?>
Keep in mind you're not doing any sanitation on the input so you might want to for security reasons. (although the plugin will negate any characters that are not numbers or slashes, it's not exactly hack proof, haha)


It's a hack, but it works. Your time will always be 00:00:00 because it's never going to be set from the calendar.

wil fucked around with this message at 20:04 on Mar 13, 2009

Fangs404
Dec 20, 2004

I time bomb.
[stuff about sqlite performance]

Edit. Nevermind. http://www.sqlite.org/speed.html suggests using BEGIN; and COMMIT; blocks, and those are working. :)

Fangs404 fucked around with this message at 02:36 on Mar 13, 2009

duck monster
Dec 15, 2004

As part of a work project I've been asked to port some basic Django/Rails type functionality into the inhouse CMS.

Oh boy. Unfortunately my prefered option of "welp, lets use django" probably wont go down well considering the company is convinced this godawful sluggish php cms it owns is worth millions in IP blah blah :sigh:

Anyway, I've written a url router, with the following code;-

php:
<?
class router {
    
    function router() {
        $this->table = array();
        $this->references = array();
    }
    function addroute ($route,$obj,$funct) {
        $this->table[$route] = array($obj,$funct);
    }

    function route() {

        foreach ($this->table as $k=>$v) {

            $urlsplit = explode(URL_SLICE,$_SERVER['REQUEST_URI']);
            $url = $urlsplit[1];
            if (preg_match_all($k,$url,$matches)) {
                global $_URL;
                $_URL = $matches;
                $obj = new $v[0]();
                print $obj->$v[1]($_SESSION,$_REQUEST,$_URL);        
            }
        }
    }
    
}
?>
Theres some fugly hacks in there, but its just prototype stuff for now.

The questions is, how does one write a regex that preg_match_all can read so that a url like

/users/32 can match on /users/ and then extract the 32 into a variable. I know in perl and python you can use named capture groups to do this , but I honestly have no loving idea how to do it in any of these, let alone php.

Any regex gurus able to help me here?

Tad Naff
Jul 8, 2004

I told you you'd be sorry buying an emoticon, but no, you were hung over. Well look at you now. It's not catching on at all!
:backtowork:

duck monster posted:

The questions is, how does one write a regex that preg_match_all can read so that a url like

/users/32 can match on /users/ and then extract the 32 into a variable.
Any regex gurus able to help me here?

Not a guru, but something like this:
php:
<?
    function route() {
        foreach ($this->table as $k=>$v) {

            $urlsplit = explode(URL_SLICE,$_SERVER['REQUEST_URI']);
            $url = $urlsplit[1];
            if (preg_match('!^/'.$k.'/(.*)$!',$url,$matches)) {
                global $_URL;
                $_URL = $matches[1];
                $obj = new $v[0]();
                print $obj->$v[1]($_SESSION,$_REQUEST,$_URL);        
            }
        }
    }
    
}
?>
$matches[0] has the whole matching string (useless), $matches[1] will have the first group, etc.

duck monster
Dec 15, 2004

FeloniousDrunk posted:

Not a guru, but something like this:
php:
<?
    function route() {
        foreach ($this->table as $k=>$v) {

            $urlsplit = explode(URL_SLICE,$_SERVER['REQUEST_URI']);
            $url = $urlsplit[1];
            if (preg_match('!^/'.$k.'/(.*)$!',$url,$matches)) {
                global $_URL;
                $_URL = $matches[1];
                $obj = new $v[0]();
                print $obj->$v[1]($_SESSION,$_REQUEST,$_URL);        
            }
        }
    }
    
}
?>
$matches[0] has the whole matching string (useless), $matches[1] will have the first group, etc.

Well yeah, obviously, the question I was asking is how to craft the regex.

Eararaldor
Jul 30, 2007
Fanboys, ruining gaming since the 1980's
Hi guys
I was just wondering if a line of php code will work with a page of javascript.

Because for my code I'm using this line to only allow people logged in to view the page.
<?php session_start(); require_once('db.php'); include('functions.php'); checkLogin('1 2'); ?>

But I'm also using javascript which I learnt from Will to provide a calendar to the form.

If I add that php code to the top I get:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/grads/public_html/computing_project/login2/booking.php:1) in /home/grads/public_html/computing_project/login2/booking.php on line 1

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Eararaldor posted:

Hi guys
I was just wondering if a line of php code will work with a page of javascript.

Because for my code I'm using this line to only allow people logged in to view the page.
<?php session_start(); require_once('db.php'); include('functions.php'); checkLogin('1 2'); ?>

But I'm also using javascript which I learnt from Will to provide a calendar to the form.

If I add that php code to the top I get:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/grads/public_html/computing_project/login2/booking.php:1) in /home/grads/public_html/computing_project/login2/booking.php on line 1

session_start() has to be the first thing you do, you can't be outputting anything before it.

Eararaldor
Jul 30, 2007
Fanboys, ruining gaming since the 1980's

duz posted:

session_start() has to be the first thing you do, you can't be outputting anything before it.

It's line 1?

wil
Mar 2, 2009
Set the session stuff up in the main file, (the one that is calling the ajax functions) not on the formProcess.php file and you should be cool.

Eararaldor
Jul 30, 2007
Fanboys, ruining gaming since the 1980's

wil posted:

Set the session stuff up in the main file, (the one that is calling the ajax functions) not on the formProcess.php file and you should be cool.

Yeah it was in the main page. Which was what I coudn't understand. Anyway I remade the page and now it works... Maybe my line 1 code was slightly wrong? Anyway doesn't matter.

*Edit one last query for you guys

I'm currently trying to make a form that updates rows on a table.

Currently my form has no issues displaying the text to be edited, in their respective text boxes however I can't make the changes seem to stick.


php:
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      $bookingID = $_GET["bookingID"];
      $sql = "SELECT * FROM booking WHERE bookingID=$bookingID";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
?>

      <form action="edit.php" method="post">
      <input type=hidden name="bookingID" value="<?php echo $myrow["bookingID"?>">     
       Room Name:<INPUT TYPE="TEXT" NAME="roomName" VALUE="<?php echo $myrow["roomName"?>" SIZE=30><br>
      <input type="hidden" name="cmd" value="edit">
      <input type="submit" name="submit" value="submit">
   
      </form>
   
   <? } ?>
<?


if ($_POST["submit"])
   {
$roomName = $_POST["roomName"];
  
 
$sql = "UPDATE booking SET roomName='$roomName' WHERE bookingID=$bookingID";

$result = mysql_query($sql);
      echo "Thank you! Information updated.";
   }
}
?>

Eararaldor fucked around with this message at 01:12 on Mar 15, 2009

duck monster
Dec 15, 2004

Big gotcha. Check for spaces, enters and cruft before the first <?

Thats often a big cause of 'headers already sent' type errors.

duck monster
Dec 15, 2004

Hmm. Maybe my regex question needs a thread

Zorilla
Mar 23, 2005

GOING APE SPIT

duck monster posted:

Hmm. Maybe my regex question needs a thread

This subforum has regexes coverered too. Post here and somebody should be able to help.

Eararaldor
Jul 30, 2007
Fanboys, ruining gaming since the 1980's

Eararaldor posted:


*Edit one last query for you guys

I'm currently trying to make a form that updates rows on a table.

Currently my form has no issues displaying the text to be edited, in their respective text boxes however I can't make the changes seem to stick.

Just in case anyones wondering, this is now fixed.

Heh, looks like it was having an issue trying to find the id row. Fixed that by adding a:
<INPUT TYPE="HIDDEN" NAME="bookingID" VALUE="<?php echo $myrow["bookingID"] ?>" SIZE=8>

Working all fine now.

Alex007
Jul 8, 2004

Eararaldor posted:

Just in case anyones wondering, this is now fixed.

Heh, looks like it was having an issue trying to find the id row. Fixed that by adding a:
<INPUT TYPE="HIDDEN" NAME="bookingID" VALUE="<?php echo $myrow["bookingID"] ?>" SIZE=8>

Working all fine now.

Three quick tips, you can can remove the SIZE attribute (since the field is hidden anyway), you can use a different php <?= ?> tag when you want to echo stuff, and you can close your input tag:

code:
<input type="hidden" name="bookingID" value="<?= $myrow["bookingID"] ?>" />

Eararaldor
Jul 30, 2007
Fanboys, ruining gaming since the 1980's

Alex007 posted:

Three quick tips, you can can remove the SIZE attribute (since the field is hidden anyway), you can use a different php <?= ?> tag when you want to echo stuff, and you can close your input tag:

code:
<input type="hidden" name="bookingID" value="<?= $myrow["bookingID"] ?>" />

Cheers for the tips

wil
Mar 2, 2009

Alex007 posted:

....you can use a different php <?= ?> tag when you want to echo stuff...



Yeah this is true, but be sure you have short_open_tag = on set in your php.ini, otherwise it won't interpret the tags :)

cannibustacap
Jul 7, 2003

Brrrruuuuuiinnssss
Are static variables only "static" for a particular end user's session, or are they static across all sessions for all users who visit your site (or use your PHP program for that matter)?

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

cannibustacap posted:

Are static variables only "static" for a particular end user's session, or are they static across all sessions for all users who visit your site (or use your PHP program for that matter)?
They are static only for that single execution of the script, not even the whole session. If you want persistent data across a user's session, use $_SESSION. If you want persistent data across all users, use a database or shared memory (memcached, APC, etc).

Vedder
Jun 20, 2006

FeloniousDrunk posted:

It was a neverending pain in the rear end for me in that situation. Generally I'd use Word's Export to HTML (different from "Save as HTML"), then HTML Tidy, then manually fix whatever goofiness remained, then grab the body of the page and throw it into the CMS. There's also "antiword" for Linux which extracts text from Word docs (I don't think anything post-Word 97), but it also strips formatting so you'd have to reapply it all over again.

Making an input page is a good idea in theory, but if people are using Word to write for the web, it's guaranteed that they will just copy from Word and then paste into the form, which leads to all sorts of misery with the curly quotes and formatting. Things like FLK and TinyMCE just exacerbate the problem, because they look like Word so people expect them to actually be Word. I hate to say it, but if you have the power to make suggestions you might want to get contributors to use a WYSIWYG HTML editor rather than Word. You'll still have to clean it up, but at least it'll arrive with the entities already done and no wacky "mso:normal" sort of tags in it.

Thought there might not be a really easy way. Is the act of tags in a database fine then? thats the first thing I thought of but thought "I bet thats a big no no".

wil
Mar 2, 2009
Vedder, FCKEditor has a built in "Paste from word" button that will strip all over the crap out and use standard html (well.. not really standard, but good enough I guess) to format it versus the Word formatting.

KarmaticStylee
Apr 21, 2007

Aaaaaughibbrgubugbugrguburgle!
Question: Have any of you used any affordable shopping cart systems that are not opensource (Zen Cart, osCommerce)? If so, which would you recommend?

Zorilla
Mar 23, 2005

GOING APE SPIT

KarmaticStylee posted:

Question: Have any of you used any affordable shopping cart systems that are not opensource (Zen Cart, osCommerce)? If so, which would you recommend?
Shopp is probably your best bet if you want to avoid really confusing templates and want integration with WordPress. I've actually been beta testing it since December. Avoid WP e-Commerce; it's free, but it's buggy and incredibly badly coded. We actually moved from WP e-Commerce to Shopp because it is designed as a replacement for it (and so did a lot of other people, as you'd find out if you hit up the Shopp forums).

Zorilla fucked around with this message at 23:48 on Mar 17, 2009

eHacked
Sep 30, 2003

CONGRATULATIONS!!! YOU ARE THE 6,127,436,218TH PERSON TO VIEW THIS USELESS POST. CLICK TO CLAIM YOUR PRIZE!!!
Ok one more stupid question:

I have many URLs that I want to forward to new, prettier URLs.

They look like this:

http://sitename.com/Site_Name-Number-Type-Article_Name/?webmaster_id=xxxx&track=TRACK

With htaccess I can pull out the "Site_Name-Number-Type-Article_Name" part and get the information I want out of that, but for whatever reason it's not getting the PHP variables in the URL "webmaster_id=xxxx&track=TRACK" ... it's completely ommiting it when passing the information.

Here's my htaccess:


RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d

RewriteCond $1 !^(.*jpg)

# RewriteRule (.*) index.php?urlVariables=$1 [QSA,L]

Any help appreciated!

edit
-------

Actually found an easy solution:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{QUERY_STRING} foo=(.*)
RewriteRule ^grab(.*) /page.php?bar=%1

Thanks anyways!

eHacked fucked around with this message at 00:14 on Mar 18, 2009

Zorilla
Mar 23, 2005

GOING APE SPIT
I'm not sure the QSA flag works as expected when you're already manually rewriting into a query string. I don't too regexes very often, so I don't know if/how well this would work:

code:
RewriteRule ^(.*)\?(.*)$ index.php?urlVariables=$1&$2
If you wanted to request page.html?webmaster_id=2165&track=TRACK, this should rewrite to index.php?urlVariables=page.html&webmaster_id=2165&track=TRACK

Zorilla fucked around with this message at 00:29 on Mar 18, 2009

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Is there a way to easily echo the compiled form of a prepared statement for PDO? I find myself frequently wanted to do this when developing and it gets annoying having to echo all the bound variables and manually replace them. I just want to quickly paste the compiled statement into phpMyAdmin or whatever.

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
Nope :|

At least I gave up trying to find a decent way after looking for a good bit.

Sylink
Apr 17, 2004

How would I test to see if a database query returned no results?

code:
mysql_select_db("cookbook", $con);

$result = mysql_query("SELECT * FROM Recipes
WHERE title='$title'");




while($row = mysql_fetch_array($result))
  {
  echo $row['ingredients'] . " " . $row['directions'];
  echo "<br />"; 
  }

This is what I'm using right now. HOw do I test if the $result has returned nothing?

I've done if ($row == FALSE) but I can't get it set up right so I can use the while loop with it.

PS Im new to php/mysql. I do know a bit about programming though.

Sylink fucked around with this message at 06:35 on Mar 20, 2009

Supervillin
Feb 6, 2005

Pillbug

Sylink posted:

How would I test to see if a database query returned no results?

What you're doing already should do that. mysql_fetch_array will return null, which means $row will be assigned null, which means the while loop will not execute.

If that doesn't seem to be working, just test if($result) outside your while loop.

Standish
May 21, 2001

Sylink posted:

How would I test to see if a database query returned no results?
php:
<?
if (mysql_num_rows($result) == 0)
?>

Sylink
Apr 17, 2004

Standish posted:

php:
<?
if (mysql_num_rows($result) == 0)
?>



This worked great. Thanks.

Sylink
Apr 17, 2004

What is the common way to set up a dynamic page with php? By this I mean I have an index.php which contains the basic layout and I have the content in separate php files.


I have seen where you can use the page variable and switch statements but I can't get it to work right. If this question is too long and anyone knows a good tutorial/resource on it I would take that.


In this case I have form included on the index page. If submitted it queries a database and returns some data. I want the search result to appear in the same html template basically.

Zorilla
Mar 23, 2005

GOING APE SPIT

Sylink posted:

What is the common way to set up a dynamic page with php? By this I mean I have an index.php which contains the basic layout and I have the content in separate php files.


I have seen where you can use the page variable and switch statements but I can't get it to work right. If this question is too long and anyone knows a good tutorial/resource on it I would take that.


In this case I have form included on the index page. If submitted it queries a database and returns some data. I want the search result to appear in the same html template basically.

Most people use a header.php and footer.php and include them at the top and bottom of each file. I usually do this instead because it's a little more flexible.

eHacked
Sep 30, 2003

CONGRATULATIONS!!! YOU ARE THE 6,127,436,218TH PERSON TO VIEW THIS USELESS POST. CLICK TO CLAIM YOUR PRIZE!!!
I'm trying to learn PHP OOP ...

I am using this online tutorial:

http://www.killerphp.com/tutorials/object-oriented-php/index.php

Everything was going great until the constructor part.

index.php has this code:

php:
<?
include("class_lib.php");
  $stefan = new person("Stefan Mischool");
  $jimmy = new person("Nick Waddles");
  
  echo "Stefan's full name: " . $stefan->get_name() . "<br />";
  echo "Nick's full name: " . $jimmy->get_name();
?>
and class_lib.php is filled with:

php:
<?
class person{
  var $name;

  function __construct($persons_name){
    $this->name = $persons_name;
  }
  function set_name($new_name){
    $this->name = $new_name;
  }
  function get_name(){
    return $this->name;
  }
}
?>
The output is:

quote:

Stefan's full name:
Nick's full name:

What the gently caress is going on? I have appx. 0 knowledge in OOP ... but the syntax looks fine to me, and no errors are returned so I'm assuming some shady stuff is going down!

And since this tutorial looks to be failing me ... can anyone else recommend a good, simple, online and free tutorial to learn OOP in PHP?

eHacked fucked around with this message at 18:36 on Mar 23, 2009

duck monster
Dec 15, 2004

That ought work. Change the include to require just to verify it is getting the person class definition.

Failing that change __construct to person , which is the alternative constructor syntax (use class name as method name) and see if that works.

eHacked
Sep 30, 2003

CONGRATULATIONS!!! YOU ARE THE 6,127,436,218TH PERSON TO VIEW THIS USELESS POST. CLICK TO CLAIM YOUR PRIZE!!!

duck monster posted:

That ought work. Change the include to require just to verify it is getting the person class definition.

Failing that change __construct to person , which is the alternative constructor syntax (use class name as method name) and see if that works.

Hi, thanks, changing the constructor name seemed to work... would this have anything to do with the server I'm learning off of running php 4?

Adbot
ADBOT LOVES YOU

spiritual bypass
Feb 19, 2008

Grimey Drawer

eHacked posted:

php 4

Don't do this to yourself. Anything before PHP5 is junk.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply