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
Summit
Mar 6, 2004

David wanted you to have this.

Roctor posted:

I don't know anything about your game, but I can share a train of thought that I've just run down if you want to avoid crons.

Say you want to build a structure with a time delay on it. Rather than saying 'add this structure to the completed structures table at X time' you would add it to the table with an activation_time field or something.

Now when you select structures from the table you have to modify your queries to only take into account buildings that are 'built', ie, have an activation_time that is <= NOW().

I don't know if any of this is helpful, but I thought I'd share. Obviously this might not apply, and it could add a level of complexity or inefficiency to your queries depending on how your db is structured.

This is actually a really good idea I hadn't thought of, thank you for sharing it. I might end up doing this if nothing else occurs to me.

Adbot
ADBOT LOVES YOU

KarmaticStylee
Apr 21, 2007

Aaaaaughibbrgubugbugrguburgle!
are there any programs or websites out there that can scan large php application files such as cakephp or wordpress for lines that send third parties sensitive information?

i know a fresh download of wordpress is fairly safe but other apps are harder to trust.

spiritual bypass
Feb 19, 2008

Grimey Drawer

KarmaticStylee posted:

are there any programs or websites out there that can scan large php application files such as cakephp or wordpress for lines that send third parties sensitive information?

i know a fresh download of wordpress is fairly safe but other apps are harder to trust.

That's a very complicated task. I recommend running a test environment and sniffing the packets that come from it.

Or I could say something about reading the code yourself or using code that comes from a trustworthy source.

KarmaticStylee
Apr 21, 2007

Aaaaaughibbrgubugbugrguburgle!

royallthefourth posted:

That's a very complicated task. I recommend running a test environment and sniffing the packets that come from it.

Thanks for the idea

royallthefourth posted:

Or I could say something about reading the code yourself or using code that comes from a trustworthy source.

Reading each and every file of open source php apps would take quite a bit of time

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
I'm looking for a third party product (free or low cost) that provides basic blog functionality (posts, comments, Atom/RSS feeds, etc) without the presentation layer. Basically something that I can dump into my own framework and incorporate into a currently existing application. Any suggestions?

niralisse
Sep 14, 2003
custom text: never ending story

supster posted:

I'm looking for a third party product (free or low cost) that provides basic blog functionality (posts, comments, Atom/RSS feeds, etc) without the presentation layer. Basically something that I can dump into my own framework and incorporate into a currently existing application. Any suggestions?

I would hope that one of the blogging apps out there had its models done well enough that you could lift them. Those are wheels that have been reinvented plenty of times.

Alex007
Jul 8, 2004

supster posted:

I'm looking for a third party product (free or low cost) that provides basic blog functionality (posts, comments, Atom/RSS feeds, etc) without the presentation layer. Basically something that I can dump into my own framework and incorporate into a currently existing application. Any suggestions?

Look at CodeIgniter, it's not a code base, it's a PHP framework, but the basic example is a full-fledged blog. You can start with that example, and add-on as you need.

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
I am already using a framework (in-house). I'm just looking to avoid having to write the mundane stuff like feed generation, comment support, etc. I guess I might consider taking a look at picking apart an already-built blog system. Are there any widely used blog plugins for CodeIgniter that I don't know about?

Alex007
Jul 8, 2004

Well, most code example you'd get for codeigniter would use a lot of codeigniter features, so you'd probably end up doing more code by converting it rather than writing from scratch.

Also, I haven't heard of any plugins, just a big tutorial and the end result is a full blog, without feeds tough.

niralisse
Sep 14, 2003
custom text: never ending story

supster posted:

I am already using a framework (in-house). I'm just looking to avoid having to write the mundane stuff like feed generation, comment support, etc.

Isn't feed generation really a presentation layer thing?

Ghotli
Dec 31, 2005

what am art?
art am what?
what.
That's what I was going to say. Ideally all the xml markup should be made in the presentation layer.

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

niralisse posted:

Isn't feed generation really a presentation layer thing?
Yeah....... good point.

My other requests still stand though.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

supster posted:

Yeah....... good point.

My other requests still stand though.

How robust of a blog system are you looking for? If it's just some basic functionality it seems like it would be less work/headache to just write your own. Do you foresee yourself updating the third party library as new versions become available? That could become a pain to merge into your concoction.

imonlyapuppet
Apr 28, 2004
keylogger
I posted this question in the Web Design megathread but I was told this is more of a PHP/MySQL question, so I'll crosspost it here, too:

I'm trying to learn how to create a search box on a web page that can search a databse and display results, for instance, searching through a product listing. I know how to make a search box that will search all the pages in a website, and the output looks like something you'd get from google. I don't want that, because if it doesn't look right and its confusing to a possible customer.

I'm looking for something like the search box in the top right of this website:

http://plasticpolybags.com/


The search box enables you to put in the Width and Height of one of these bags, and when you submit it, it will display all the possible product listings that fit those dimensions, it won't just start listing any page that has the word "height" or "width" or any random number in it.

I don't know if this is a MySQL thing, an HTML thing, or a PHP thing, so I'm hoping you goons can point me in the right direction.

It seems like designing a search box like this is a little time consuming and complicated, so if you can't provide an answer on how to create one of these, can you at least kind of explain the process behind what's going on when you click Submit on this search box? What are some websites or resources I could use to try to learn how to replicate this search box on my own?

I'm pretty computer/internet savvy, just not programming/coding savvy...

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

imonlyapuppet posted:

The search box enables you to put in the Width and Height of one of these bags, and when you submit it, it will display all the possible product listings that fit those dimensions, it won't just start listing any page that has the word "height" or "width" or any random number in it.

Basically you'd have a table in a database (think of a table as spreadsheet in excel). Let's call this table Bags. We'd have a few fields (or columns, in excel) called Name, Width, Length, (Id, Type, etc). Let's say we have some data like:

code:
Name		Width		Length
Clear Bag	10		20
Brown Bag	15		10
Blue Bag	12		6
The width, length, and type inputs on that example page you posted are part of an HTML element called a <form> where you enter in data, hit submit, and it sends it to a server. Once it goes to the server, you can use PHP to read what the user entered, and using that you can query the database to bring back the matching bags like:

select Name from Bags where Width=10 and Length=20

and you basically replace 10 and 20 in that statement with variables containing the value of what the user entered.

There's a whole slew of information you can read about how to do this using php. XAMPP is an easy way to get an environment running for writing a PHP/MySQL app for the first time.

cannibustacap
Jul 7, 2003

Brrrruuuuuiinnssss
Is there a way to make the "substr" function act more dynamically so that it will only cut off at the end of a word (or before a space or end of string) as apposed to exact at XYZ charters?

for example

$str = "hey there!"

If I say

substr($str,0,5)

It will produce "hey t"

Is there a function that will work as an exception to this that will allow you to bleed over a word so things don't cut off unnaturally?

Perhaps

smartsubstr($str,0,5) will product either "hey" or fully "hey there" but it won't cut it off in the middle of a word?

Internet Headache
May 14, 2007
php:
<?
function substr_words($str, $limit) {
    if (strlen($str) <= $limit) {
        return $str;
    }
    preg_match('/^.{'.($limit - 1).'}\S*/us', $str, $matches);
    return $matches[0];
}
?>

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

cannibustacap posted:

smartsubstr($str,0,5) will product either "hey" or fully "hey there" but it won't cut it off in the middle of a word?

You can write your own pretty easily by using explode with " " as your delimiter.

cannibustacap
Jul 7, 2003

Brrrruuuuuiinnssss

cannibustacap posted:

Is there a way to make the "substr" function act more dynamically so that it will only cut off at the end of a word (or before a space or end of string) as apposed to exact at XYZ charters?

for example

$str = "hey there!"

If I say

substr($str,0,5)

It will produce "hey t"

Is there a function that will work as an exception to this that will allow you to bleed over a word so things don't cut off unnaturally?

Perhaps

smartsubstr($str,0,5) will product either "hey" or fully "hey there" but it won't cut it off in the middle of a word?

Okay I got off my lazy rear end and did it. Confirmed to work:

php:
<?
  if ($body && strlen($body) >= $limit) {
    $words = explode(' ', $body);
    for ($k = 0, $count = 0; $count <= $limit && $k < sizeof($words); $k++, $count += strlen($words[$k])) {
      $sent   = $sent ? $sent . ' ' . $words[$k] : $words[$k] . ' ';
    }
  }
?>
Thanks for the exploding idea :)

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker

cannibustacap posted:

Okay I got off my lazy rear end and did it. Confirmed to work:

php:
<?
  if ($body && strlen($body) >= $limit) {
    $words = explode(' ', $body);
    for ($k = 0, $count = 0; $count <= $limit && $k < sizeof($words); $k++, $count += strlen($words[$k])) {
      $sent   = $sent ? $sent . ' ' . $words[$k] : $words[$k] . ' ';
    }
  }
?>
Thanks for the exploding idea :)

I can't even read what the gently caress that's supposed to do. :psyduck: Maybe it's because I'm drowsy as hell (very probable) but don't you find this far easier to read?

php:
<?
$max = 15;

$output = '';
$count = 0;

$words = explode(' ', $body);
foreach($words as $word)
{
    if( strlen($output) >= $max)
    {
        $output = trim($output);
        break;
    }
    $output .= $word . ' ';
}

?>

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

Mercator posted:

I can't even read what the gently caress that's supposed to do. :psyduck: Maybe it's because I'm drowsy as hell (very probable) but don't you find this far easier to read?

But your's is almost 3x the lines...... :colbert:

savetheclocktower
Sep 23, 2004

You wait and see, Mr. Caruthers. I will be president! I'll be the most powerful president in the history of America. And I'm gonna clean up this country!

supster posted:

But your's is almost 3x the lines...... :colbert:

You are clearly old enough to remember The Great Carriage-Return Shortage of Aught-Three.

No, seriously. Go with the more readable code. Six months from now you won't remember what the gently caress this does and will need to figure it out just from reading it.

KuruMonkey
Jul 23, 2004

savetheclocktower posted:

No, seriously. Go with the more readable code. Six months from now you won't remember what the gently caress this does and will need to figure it out just from reading it.

If only PHP had some feature where you could inject extra text into your code that the interpreter would ignore. You could use this, in a pinch, to annotate your program. At a stretch you could possibly even explain what you've done...

cannibustacap
Jul 7, 2003

Brrrruuuuuiinnssss
Guys its very easy to read


php:
<?
  if ($body && strlen($body) >= $limit) {
    $words = explode(' ', $body);
    for ($k = 0, $count = 0; $count <= $limit && $k < sizeof($words); $k++, $count += strlen($words[$k])) {
      $sent   = $sent ? $sent . ' ' . $words[$k] : $words[$k] . ' ';
    }
  }
?>
Line 1: If body is a variable and is length is better than some limit, then we go in.
Line 2: Words is an array made of each separate "word" in the body.
Line 3: create a for loop with two conditions: k, count. Both start at 0, but k increases incrementally to keep track of the keys, count just keeps track of the numbers of number of characters used so far.

Once that limit is reached, you have $sent, your $sentence.

Then again, sometimes I look at my co-workers code and have no clue what they say either. I really should comment more :)

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
Might want to use something like this if you ever have to cut off extremely long strings.
php:
<?
function cutoff($string, $limit)
{
    if(strlen($string) <= $limit)
        return $string;

    $pos = $limit;
    while($pos > 0 && $string[$pos] != ' ' && $string[$pos] != "\t" && $string[$pos] != "\n")
        $pos--;
    return rtrim(substr($string, 0, $pos));
}
?>

jasonbar
Apr 30, 2005
Apr 29, 2005

cannibustacap posted:

Guys its very easy to read

php:
<?
  if ($body && strlen($body) >= $limit) {
    $words = explode(' ', $body);
    for ($k = 0, $count = 0; $count <= $limit && $k < sizeof($words); $k++, $count += strlen($words[$k])) {
      $sent   = $sent ? $sent . ' ' . $words[$k] : $words[$k] . ' ';
    }
  }
?>
Then again, sometimes I look at my co-workers code and have no clue what they say either. I really should comment more :)
No, it's easy to understand, it's terrible to read. It also happens to be incorrect.

$sent will usually be a string of length $limit + $k + 1 AND have two spaces after the first word. Additionally, you are ignoring the length of the first word entirely!

Try running this with the string "One two one two one two" and a limit of 6. You get one whole extra word. (This breaks on any strings that have the correct sequence of words and when it does your strlen goes through the roof)

Use what supster wrote, or:
php:
<?
if (strlen($body) <= $limit)
    $sent = $body;
else
{
    $sent = wordwrap($body, $limit, "SOMETHING_THAT_WILL_NEVER_APPEAR_IN_THE_TEXT");
    $sent = substr($sent, 0, strpos($sent, "SOMETHING_THAT_WILL_NEVER_APPEAR_IN_THE_TEXT"));
}
?>
They will both give you the same string, which will have a length <= $limit.

edit: gently caress comments.

jasonbar fucked around with this message at 18:19 on Feb 14, 2009

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker
Every solution written so far is terrible since it doesn't add an ellipsis at the end of the chopped string.

cannibustacap
Jul 7, 2003

Brrrruuuuuiinnssss
Well for the purpose of my web site, this actually does work good enough.

Its all rough estimates anyway.

Jo3sh
Oct 19, 2002

Like all girls I love unicorns!
I'm writing a PHP and MySQL based inventory system for all the beer I have around. I'm able to insert records into the database just fine, and I can display them in a table just fine. I'd like to have a button by each row with the label, "Remove this Beer," which will call another php page to confrim deletion of the beer from the database.

When I insert records into the database, I create a UID for each row using uniqid(), and I need to make sure I'm passing that UID so I know I am deleting the right row.

Here's what I have so far:

code:
// snip the part where I connect to the database and retrieve the table

echo "<table border='1'>
<tr>

<th>Brewer or Brewery</th>
<th>Name of Beer</th>
<th>Size</th>
<th>GUID</th>
</tr>";

while($row = mysql_fetch_array($result))
 {
 echo "<form name=\"form1\" method=\"post\" action=\"removebeer.php\">";
 echo "<tr>";
 echo "<td>" . $row['brewer'] . "</td>";
 echo "<td>" . $row['name'] . "</td>";
 echo "<td>" . $row['size'] . "</td>";
 echo "<td><input type=\"submit\" name=\"".$row['guid']."\" value=\"Remove this Beer"></td>";

 echo "</tr>";
 echo "</form>";
 }
echo "</table>";

Clearly, then, this creates a series of buttons whose names are unique. How can I pass that unique string to removebeer.php?

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine
Two methods:

1) Instead of a button, have a link that goes to "removebeer.php?id=$guid" or whatever. That way, no form is necessary at all.

2) Have a hidden input field. "<input type='hidden' name='beerid' value='555'>" and then it will be $_POST['beerid'], but the user will never see it.

As an aside, why are you using uniqid() instead of having an autoincrement ID field in the database?

Jo3sh
Oct 19, 2002

Like all girls I love unicorns!

Golbez posted:

Two methods:

1) Instead of a button, have a link that goes to "removebeer.php?id=$guid" or whatever. That way, no form is necessary at all.

2) Have a hidden input field. "<input type='hidden' name='beerid' value='555'>" and then it will be $_POST['beerid'], but the user will never see it.

As an aside, why are you using uniqid() instead of having an autoincrement ID field in the database?

Excellent, thank you. I'll be using the second method you suggested.

As to uinqid(), it seemed like a good idea at the time and now I am kind of committed to it for now. You're right that autoincrement would do it, and I have no idea why I didn't think of that at the start.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
edit: I typed out this whole post, and then stumbled upon the way to do it myself...

Hammerite fucked around with this message at 01:46 on Feb 15, 2009

zeldadude
Nov 24, 2004

OH SNAP!
I apologize if this has been asked before but it's hard to search for a question like this:

Is there any way to make it so pages that you only want to be included (such as a header.php, or navigation.php) can't be viewed unless in an include?

Guess it applies to just scripts too such as search.php, I don't want people just typing in search.php and seeing it.

Wow, that was harder than I thought to describe.

gibbed
Apr 10, 2006

The smart way is to put any important source files outside of webroot, period.
The dumb way is to do something like, if (!defined('MY_APP_BE_RUNNIN')) { return; } in your header fine, and define('MY_APP_BE_RUNNIN', true); in any files that include it.

zeldadude
Nov 24, 2004

OH SNAP!

gibbed posted:

The smart way is to put any important source files outside of webroot, period.
The dumb way is to do something like, if (!defined('MY_APP_BE_RUNNIN')) { return; } in your header fine, and define('MY_APP_BE_RUNNIN', true); in any files that include it.

How about I do both? :clint:

I'm working on a clothing shop website with PHP and MySQL currently, and it's my first real project. I know nothing about security. Hopefully I won't learn the hard way :v:

Zorilla
Mar 23, 2005

GOING APE SPIT

Jo3sh posted:

Clearly, then, this creates a series of buttons whose names are unique. How can I pass that unique string to removebeer.php?

Rrraaagh echo statements for HTML! A pet peeve of mine. Any reason you're not doing this?

php:
<?php
// snip the part where I connect to the database and retrieve the table
?>
<form name="form1" method="post" action="removebeer.php">
    <table border="1">
        <tr>
            <th>Brewer or Brewery</th>
            <th>Name of Beer</th>
            <th>Size</th>
            <th>GUID</th>
        </tr>
<?php while($row mysql_fetch_array($result)) { ?>
        <tr>
            <td><?=$row['brewer']?></td>
            <td><?=$row['name']?></td>
            <td><?=$row['size']?></td>
            <td><input type="submit" name="<?=$row['guid']?>" value="Remove this Beer" /></td>
        </tr>
<?php // while ?>
    </table>
</form>
By the way, when fixing your code, I also noticed you're looping the form over and over again. That can't be good.

Zorilla fucked around with this message at 10:06 on Feb 15, 2009

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

Zorilla posted:

By the way, when fixing your code, I also noticed you're looping the form over and over again. That can't be good.

Not necessarily. If he wanted a distinct submit button for each line, creating a new form for each line is the way to do it, and so far as I can tell has no deleterious effects. Now, sure, your method works too, since the value is included in the name of the submit button, but it doesn't work if each row had an editable field or what not.

edit: It could work if each row had a unique name, so you could do "<input name='name<?=$row['guid']?>'", I suppose.

Golbez fucked around with this message at 17:33 on Feb 15, 2009

Zorilla
Mar 23, 2005

GOING APE SPIT

Golbez posted:

Not necessarily. If he wanted a distinct submit button for each line, creating a new form for each line is the way to do it, and so far as I can tell has no deleterious effects. Now, sure, your method works too, since the value is included in the name of the submit button, but it doesn't work if each row had an editable field or what not.

One of the things I meant that it was invalid HTML to put <form> tags inside <table>. You either have to start and stop them outside <table> or put them inside <td>.

Besides, it's still bad practice to use multiple forms on a page where all the information looks like it belongs to one form. What happens when you edit information on one row, then hit Submit on another? You lose the other row. Not good.

If he were to end up using text input fields later on like you brought up, that's when he should start considering using arrayed input names like description[somenumber] and checkboxes for deletion instead.

Zorilla fucked around with this message at 20:28 on Feb 15, 2009

KuruMonkey
Jul 23, 2004

Zorilla posted:

Rrraaagh echo statements for HTML! A pet peeve of mine.

Got any rational reason behind having that pet peeve?

Since my preference runs the exact opposite to yours for most involved html rendering, I'd be interested if there was an actual reason to do things one way rather than the other; I'm not aware of a reason using echo is 'wrong'?

Adbot
ADBOT LOVES YOU

Zorilla
Mar 23, 2005

GOING APE SPIT

KuruMonkey posted:

Got any rational reason behind having that pet peeve?

Since my preference runs the exact opposite to yours for most involved html rendering, I'd be interested if there was an actual reason to do things one way rather than the other; I'm not aware of a reason using echo is 'wrong'?
I'm surprised anyone has to ask. Look at Jo3sh's code, then look at mine. Which is easier to look at?

Using echo statements:
  • Produces lots of code garbage that clutters up HTML like quotation marks, escape characters, \n, semicolons, the word "echo" all over the place, etc.
  • Doesn't color-code the HTML in editors like Notepad++, TextMate, Dreamweaver, etc.
  • You're probably not tabbing the output or using line breaks at the end of each string, so you end up with poo poo like <table><tr><td>Poop</td><td><table><tr><td>Holy poo poo a table within a table</td></tr></table></td></tr></table> when you view source
Really, MVC code is best, but for projects way too small for frameworks like Zend or CodeIgniter, decent output templating is still strongly encouraged even if there is processing in the same file. Someone may want to edit the layout in the future and it's a real bitch to do that if everything is an echo statement.

Zorilla fucked around with this message at 20:31 on Feb 15, 2009

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