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
butt dickus
Jul 7, 2007

top ten juiced up coaches
and the top ten juiced up players

Golbez posted:

Hm. Just wondering, is it necessary to run a mysql_num_rows > 0 before doing a while $x = mysql_fetch_assoc? I suppose it might be cleaner, but if zero rows were returned, then the while wouldn't even attempt to run it, right?

Correct, but you might want to let the user know that the query returned no rows.

Adbot
ADBOT LOVES YOU

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

Doctor rear end in a top hat posted:

Correct, but you might want to let the user know that the query returned no rows.

Right, and if I were doing anything there that wasn't inside the while() I'd do the mysql_num_rows() check, but otherwise I can skip that part.

KuruMonkey
Jul 23, 2004

Golbez posted:

KuruMonkey's str_repeat is probably an ever-so-slightly 'purer' solution. But this sounds like a very strange error on sqlsrv_query's part.

I'd be surprised if it didn't actually call the same underlying code, really.

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
(Sorry for the rapid fire questions, I'm doing a review/cleanup of my own code :P) Am I a bad person for using double bangs?
code:
return mysql_num_rows($rQuery)?true:false;
vs.
code:
return !!mysql_num_rows($rQuery);
?

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Golbez posted:

(Sorry for the rapid fire questions, I'm doing a review/cleanup of my own code :P) Am I a bad person for using double bangs?
code:
return mysql_num_rows($rQuery)?true:false;
vs.
code:
return !!mysql_num_rows($rQuery);
?

you just want to get "true" or "false"? Why not just cast to Boolean:

return (bool)mysql_num_rows($rQuery);

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

Hammerite posted:

you just want to get "true" or "false"? Why not just cast to Boolean:

return (bool)mysql_num_rows($rQuery);

Four extra characters. :colbert: (also, thanks for showing me the existence of the 'fixed' tag, that is useful)

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Golbez posted:

Four extra characters. :colbert:

But far less likely to give someone else who looks at your code a "WTF" moment.

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

Hammerite posted:

But far less likely to give someone else who looks at your code a "WTF" moment.

Oh fine. And I asked the other coder here if I was a bad person for doing it and he said yes, so I guess that settles it. :eng99:

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



KuruMonkey posted:

Given that it looked like your code was building some large strings in its tests; have you tried running the tests in isolation, or even just in the opposite order?

I mean; if you want to test the faster way to create longish strings in a large loop, you first test the faster loop method in isolation (doing something constant and simple that doesn't chew up memory), then the faster concatenation method in isolation (by using the same loop method throughout)

I think you'll get a result of foreach + implode being best.

Note: in which case its worth testing array_map() as well?

Good idea - I should have thought to time just the loops and that's what I was talking about when I was asking about validity. I can see if PHP can optimize out a loop with an empty block because I've found those in our code.

I've been assuming that the overhead involved in calling an interpreted function (as opposed to an internal C implementation) would make array_map somewhat slower than implode, but the assumption I made about using count inside a loop was wrong so who knows.

Edit: I did something stupid and the long-running loops were behaving that way because they were using the length of the generated string as a limit instead of the array length. Dunno how many times I looked at that and said "Yeah, that's right, so what the gently caress" :\

Equality checks didn't see it because $array[$out_of_bounds_index] coerces to an empty string. Gaah.

A for loop with the limit pre-computed turns out to be about the same speed as a foreach and they're ~180% slower than good old implode(on 5.2.5 on Windows).

Munkeymon fucked around with this message at 23:06 on Aug 17, 2010

boak
Aug 17, 2010

im gay
How can I check if there is anything in STDIN before reading from it?

if ( ! feof(STDIN)) always returns TRUE

Basically I have a while loop in a script that does fgets(STDIN), but if nothing is there it waits for input before continuing.

EDIT

after more research I've found out that what I'm after is a "non-blocking read from STDIN".

Basically my script is a wrapper for another program. I have a while loop that reads the output from the program. In the while loop I want to get any input from STDIN and send it to the program. My first attempt with fread/fgets halts execution until input is entered which obviously isn't desired.

I want the wrapper to run as normal, however if there is any input in STDIN, be able to capture that and send it off to the program.

Can anyone point me in the right direction?

boak fucked around with this message at 08:29 on Aug 18, 2010

Flamadiddle
May 9, 2004

Can someone just give me some quick pointers on best practice (maybe even recommend a book as I'd like to learn this stuff properly) with regards to laying out my code? Specifically, I want to know where to put validation/database queries/page generation code.

Currently, the page which users are visiting to use my app is getting GET parameters, passing those off to a validation function to be checked, passing them onto a verification function to verify the login, passing some parameters to a query function to get the results of the request and so on... As I'm primarily a Java developer, it makes sense to me to move these things to separate functions, it makes the logic of the page easy to follow as you don't get blinded by the implementation, etc.

There's a form on the page, which currently POSTs to the same page so I can validate the form and display error messages, so that's another request type that needs to be handled by the same page and so would become its own function, called dependent on whether a POST parameter was set.

Anyway, was just wondering if anyone had any tips on best practice for keeping this stuff tidy. I'm packing all my session data (IP Addresses, GET/POST Parameters) into an object to distribute to these functions as that seemed sensible, but I'm not particularly experienced in the PHP way as yet.

boak
Aug 17, 2010

im gay
It typically depends on what your application is, if you're using a framework, are you using OOP etc.

For example, I have a website CMS written in CodeIgnitor, so it follows an MVC structure as well as some other minor CI-specific organisational stuff.

Choosing how to best layout and break up your code is like choosing the best web host - each has its own pros and cons that you need to weigh up. There isn't one hard and fast "best practice" and it is difficult to get good advice from someone who doesn't know your code as well as you.

Best thing to do would be look at a couple of different methods and choose one that suits.

sonic bed head
Dec 18, 2003

this is naturual, baby!
The Zend Framework is obnoxious with its lack of good documentation! I am trying to figure out how to autoload my models directory and it just isn't working out for me. Does anyone have any good resources for this or any idea what to do? I have the standard setup of controllers and view/scripts and layout/scripts directories. For some reason even require_once '../models/MyModel.php' isn't working;

Edit: require once is working, I just had to prepend dirname(__FILE__) to everything. Isn't this what AutoLoading is built for?

sonic bed head fucked around with this message at 03:33 on Aug 19, 2010

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
It's time for another pointless image manipulation question! I'm doing what Alex007 helpfully pointed out and using the output buffer and imagejpeg to save an image in a variable. So I do this...
code:
ob_start();
imagejpeg($rNewImg, null, 100);
$sImg = ob_get_contents();
ob_end_clean();

$iSize = strlen($sImg);
And in the case of this particular image it's reporting 642904 bytes. But when I check the image size in Firefox, its size is 603945 bytes, a disparity of 40k, which confuses me. Furthermore, when I download the image, the actual size is 643018 bytes. I just checked the length of the blob in MySQL (yes, I know, fire and brimstone) and it's also 642904.

So, I'm completely confused. Why is Firefox wrong? Is strlen not working right in this case? I've checked, I don't appear to have the mbstring overloading enabled. Is the size difference between strlen() and actual simply the difference in headers?

Edit: And I just had imagejpeg write the image to the filesystem instead and filesize() returns what strlen() returns, 642904 bytes. So that appears to be the 'real' size of the image, so why is it 200 bytes larger when downloaded (that's the actual size, not the 'size on disk') and why is Firefox's size so far off?

Corollary: Is there any point to using imagejpeg() to display an image in cases where I can simply echo the already-stored image data?

Golbez fucked around with this message at 15:51 on Aug 19, 2010

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.

sonic bed head posted:

The Zend Framework is obnoxious with its lack of good documentation! I am trying to figure out how to autoload my models directory and it just isn't working out for me. Does anyone have any good resources for this or any idea what to do? I have the standard setup of controllers and view/scripts and layout/scripts directories. For some reason even require_once '../models/MyModel.php' isn't working;

Edit: require once is working, I just had to prepend dirname(__FILE__) to everything. Isn't this what AutoLoading is built for?

Compared to most frameworks, the Zend Framework actually has very good documentation.

Have you added your namespace to the autoloader? http://framework.zend.com/manual/en/zend.loader.html

I believe your stuff should be in the following structure where library is the same folder where you have Zend (this is from memory from the last time I used Zend)

/library/My/Model/User.php

In this case your project is called My, that is your namespace, then you have a Model folder and then a user model. The actual class should be called My_Model_User.

You can have a different structure than this if you modify the autoloader, or decide to use your own autoloader.

Zend framework has, by design, very few interdependencies. You should be able to use your own autoloader, or extend Zend_Loader with few issues, and the rest of the Zend Framework should work just fine.

Alex007
Jul 8, 2004

Golbez posted:

(...) so why is it 200 bytes larger when downloaded (that's the actual size, not the 'size on disk') and why is Firefox's size so far off?

How do you re-output your image from the DB ? If you ImageCreateFromString() -> ImageJpeg() then there's a big chance PHP is reencoding your image, changing image quality or other setting like that. If you just output the string, there shouldn't be any difference. I'd go with this (echo the DB field) personally.

In FF, I'd Right-Click > Save As the image and save it to disk, then do a binary compare with the original file (or blob) to see where the difference is. My guess is that the image got recoded, or stuff (blanks?) gets appended to the image because you script outputs it after the image.

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

Alex007 posted:

How do you re-output your image from the DB ? If you ImageCreateFromString() -> ImageJpeg() then there's a big chance PHP is reencoding your image, changing image quality or other setting like that. If you just output the string, there shouldn't be any difference. I'd go with this (echo the DB field) personally.

In FF, I'd Right-Click > Save As the image and save it to disk, then do a binary compare with the original file (or blob) to see where the difference is. My guess is that the image got recoded, or stuff (blanks?) gets appended to the image because you script outputs it after the image.

Heh. Yep, when I just do an echo straight from the database rather than using imagejpeg, the disparity disappears. Nothing being output after the image, there's a die() right after it, so I'm not sure where FF's earlier inflation came from, but at least now everything's all right. Thanks!

excidium
Oct 24, 2004

Tambahawk Soars
I'm currently setting up a very basic reporting/tracking system for my job and have everything ALMOST complete. It's pretty simple input into a DB and a display page where values are shown. We basically are just pulling numbers, but there is a need for a News section. I set up a new DB table for the news with the columns: ID, Time, Date, Subject, FullText.

On the display page I am showing a table of all the News from the current day with Time and Subject. I would like to be able to set it up so the user can either click on the table cell or the Subject text and open a pop-up with the full text. I can't figure out the right way to do this though. Any suggestions? I'm using PHP5 and mySQL.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

excidium posted:

I'm currently setting up a very basic reporting/tracking system for my job and have everything ALMOST complete. It's pretty simple input into a DB and a display page where values are shown. We basically are just pulling numbers, but there is a need for a News section. I set up a new DB table for the news with the columns: ID, Time, Date, Subject, FullText.

On the display page I am showing a table of all the News from the current day with Time and Subject. I would like to be able to set it up so the user can either click on the table cell or the Subject text and open a pop-up with the full text. I can't figure out the right way to do this though. Any suggestions? I'm using PHP5 and mySQL.

Quick and dirty: hide the FullText for each entry in a <input type="hidden"/> that lives inside the cell they are clicking on. Use jQuery to display it when they click on the subject:

code:
$(document.ready(function() {
    $(".subjectCell").click(function() {
        var fullText = $(this).find("input[type=hidden]").val();
        alert(fullText);
    });
});
Of course you will probably want to do something besides just an alert(), a jQuery plugin would probably let you do something pretty snazzy and easily.

excidium
Oct 24, 2004

Tambahawk Soars
Well without adding jQuery I just stopped being a retard and made my links dynamic with the $_GET functionality.

So my code now looks like this:
code:
		<table id="news">
			<thead>
				<th colspan="2">NEWS: <?php echo date("l, F dS, Y"); ?></th>
			</thead>
			<tbody>
				<?php while($newsRow = mysql_fetch_array($newsResult))
				{
					echo "<tr>";
					echo "<td>" . $newsRow['Time'] . "</td>";
					echo "<td><a href=\"news.php?id=".$newsRow['ID']."\">".$newsRow['Subject']."</a></td>";
					echo "</tr>";
				}
				?>
			</tbody>
		</table>
I just need to mess with the link opening a sizable window - but it pulls up the right article each time at least!

epswing
Nov 4, 2003

Soiled Meat
Seeing that makes me itch. I use alternative syntax for control structures when I can, just feels cleaner, less html polluting your code (or code polluting your html, depending on how you look at it).

php:
<?
<table id="news">
    <thead>
        <th colspan="2">NEWS: <?php echo date("l, F dS, Y"); ?></th>
    </thead>
    <tbody>
        <?php while($newsRow mysql_fetch_array($newsResult)): ?>
        <tr>
            <td><?php echo $newsRow['Time'?></td>
            <td><?php echo "<a href=\"news.php?id=".$newsRow['ID']."\">".$newsRow['Subject']."</a>" ?></td>
        </tr>
        <?php endwhile; ?>
    </tbody>
</table>
?>

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Don't be afraid of jQuery! You can do some really cool stuff with very little effort.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Have any of you ever written a "custom application" for Invision Board? I'm trying my hand at it but the documentation isn't as full as I'd hoped.

excidium
Oct 24, 2004

Tambahawk Soars

epswing posted:

Seeing that makes me itch. I use alternative syntax for control structures when I can, just feels cleaner, less html polluting your code (or code polluting your html, depending on how you look at it).

Thanks for the tip. My code is definitely not pretty, but it's functioning for now!

excidium
Oct 24, 2004

Tambahawk Soars
OK this is most likely another retarded question but I can't seem to find what I'm looking for.

I'm adding records to my DB with a time stamp using the NOW() function. This inserts the time as h:m:s using the 24 hour military time. That's fine, but when I query and want to display the time back, I want to show it in h:m am/pm. Is there a function to do this, or do I just need to set up logic to sub-string, subtract and add AM/PM as necessary?

Alex007
Jul 8, 2004

excidium posted:

OK this is most likely another retarded question but I can't seem to find what I'm looking for. (...)

MySql, MsSql or PostgreSQL ? What is the field type you are using ?

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

excidium posted:

OK this is most likely another retarded question but I can't seem to find what I'm looking for.

I'm adding records to my DB with a time stamp using the NOW() function. This inserts the time as h:m:s using the 24 hour military time. That's fine, but when I query and want to display the time back, I want to show it in h:m am/pm. Is there a function to do this, or do I just need to set up logic to sub-string, subtract and add AM/PM as necessary?

php:
<?
date( "g:H a",  strtotime( $militaryTime ) );
?>

excidium
Oct 24, 2004

Tambahawk Soars
Great, thanks Lumpy!

php:
<?php while($newsRow mysql_fetch_array($newsResult)): ?>
 <tr>
  <td><?php echo date("g:i a"strtotime($newsRow['Time'])) ?></td>
 </tr>
<?php endwhile; ?>

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
You could also add a TIME_FORMAT() to the query, if your DBMS has said function.

Fehler
Dec 14, 2004

.

drcru posted:

Have any of you ever written a "custom application" for Invision Board? I'm trying my hand at it but the documentation isn't as full as I'd hoped.
Only for 2.3, and I guess you are using IP.Board 3? Usually their documentation is pretty good though, at least compared to some other stuff I worked with...

DoctorScurvy
Nov 11, 2005
More of a passing curiosity, really

excidium posted:

OK this is most likely another retarded question but I can't seem to find what I'm looking for.

I'm adding records to my DB with a time stamp using the NOW() function. This inserts the time as h:m:s using the 24 hour military time. That's fine, but when I query and want to display the time back, I want to show it in h:m am/pm. Is there a function to do this, or do I just need to set up logic to sub-string, subtract and add AM/PM as necessary?
Alternatively you could retrieve the field from mysql using "SELECT UNIX_TIMESTAMP(`timestamp`) AS `timestamp` FROM table" and then do all of your formatting with php's date()

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Fehler posted:

Only for 2.3, and I guess you are using IP.Board 3? Usually their documentation is pretty good though, at least compared to some other stuff I worked with...

Yep, IP.Board 3, it seems like quite a bit has changed from version 2 but at least it looks like you don't have to have people edit any files now.

uh zip zoom
May 28, 2003

Sensitive Thugs Need Hugs

could any of you guys recommend a decent IDE for PHP/MySQL projects. I am most familiar with c#/MSSQL and Visual Studio in general. I'm really having trouble debugging my projects, any suggestions?

Also, what's worthwhile when it comes to PHP frameworks?

epswing
Nov 4, 2003

Soiled Meat
I used eclipse with the web-tools-platform for a long time, but became too frustrated with it. I currently use and like netbeans, it does everything I want, including debugging.

uh zip zoom
May 28, 2003

Sensitive Thugs Need Hugs

epswing posted:

I used eclipse with the web-tools-platform for a long time, but became too frustrated with it. I currently use and like netbeans, it does everything I want, including debugging.

thanks for the tip. I recall using netbeans for a Java/OOP class a few years back, I'll give it another whirl.

sonic bed head
Dec 18, 2003

this is naturual, baby!

Begby posted:

Compared to most frameworks, the Zend Framework actually has very good documentation.

Have you added your namespace to the autoloader? http://framework.zend.com/manual/en/zend.loader.html

I believe your stuff should be in the following structure where library is the same folder where you have Zend (this is from memory from the last time I used Zend)

/library/My/Model/User.php

In this case your project is called My, that is your namespace, then you have a Model folder and then a user model. The actual class should be called My_Model_User.

You can have a different structure than this if you modify the autoloader, or decide to use your own autoloader.

Zend framework has, by design, very few interdependencies. You should be able to use your own autoloader, or extend Zend_Loader with few issues, and the rest of the Zend Framework should work just fine.

What I don't understand about this is why would I create my own library for this when the framework's own IDE has created a "models" directory as a child of "application"? Is that directory supposed to be empty and am I supposed to put all of my own non-controller code in my own library directory?

Thank you so much for your help. You are much clearer than the docs, I think. I'm used to the beautiful code igniter documentation and even those are nothing compared to something like jQuery or ExtJS.

halonx
May 4, 2005

I'm creating a site which I want to encrypt some data using AES or some other two way encryption and I don't want the key to be stored anywhere on the server.

When a user signs in, I need the user to either provide the key or retrieve the key from somewhere in a way that wouldn't be easily reproducible by a hacker or someone who has access to the source code.

Does anyone have a good idea for how to do this? I've been racking my brain for a couple days and have only come up with half assed ideas.

boak
Aug 17, 2010

im gay
Can't you just have the user submit the key over https with a form or cookie or something

halonx
May 4, 2005

boak posted:

Can't you just have the user submit the key over https with a form or cookie or something

I'd prefer the user not actively know they are giving me the encryption key. It would be like using their password as the key, but then if they change their password( something I only keep hashed in the db, so I have no idea what it is ), then all of a sudden they can't decrypt the sensitive data.

Adbot
ADBOT LOVES YOU

boak
Aug 17, 2010

im gay
Unless the user remembers the key (meaning the key is probably weak and probably unsuitable for what you want to use it for) it will need to be stored somewhere (cookie, client-side or server-side) or it'll need to be able to be generated easily (something like doing a hash of their user-agent or something), but this would be easily faked.

I'm not sure you will be able to get what you're after without some kind of trade off.

Without know anything about your application, I would have the user only submit the key when it is needed to decrypt something and discarded once decryption is finished. Not nearly perfect but there are going to be a number of risks no matter which route you decide to take.

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