Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Quick bit of PHP help :)
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Quick bit of PHP help :)

AutoSnipeAutoSnipe Member
edited February 2014 in Help

hey there, im working on a little project at the moment to create some reports a bit easier...
but im a bit stuck currently....
wondering if someone could give some ideas..

so far i have this.. but it still doesn't work quite well for me..
i am trying to make a table of asset classes and how much people have within the assets..
but only trying to make the "Amount in %" column appear once it appear ONCE in the while loop.


$class = mysql_query("SELECT DISTINCT assetclass FROM asset WHERE clientid = '$cid'");
while($cres = mysql_fetch_object($class)){
    foreach($cres as $aclass){
  
...... *Random bits and pieces that have no affect here..... 

$q2 = mysql_query("SELECT * FROM asset WHERE clientid = '$cid' AND assetclass = '$cres->assetclass'");
 while($s = mysql_fetch_object($q2))
 {
 $i = 0;
    if($cres->assetclass == 'asx'){
        $price = mysql_query("SELECT * FROM asxcode WHERE code = '$s->assetname'");
        $c = mysql_fetch_array($price);
        $tot = $c['price']*$s->assetamount;
 $asxprice += $tot;
 $total = $asxprice;
  if(!empty($asxprice)){
    $result .= " $tablename ";    
    $result .= " $c[name] ($s->assetname)";

    $result .=  "$tot";
    }
    if($i == 0)
    $result .=  "*percent Function* %";
    }
    $i++;
    
    

currently it appears as this

image

anyone got some ideas for me?

Comments

  • hostnoobhostnoob Member
    edited February 2014

    edit: nvm. still looking will update if I notice anything

  • Easy, introduce a tertiary variable like $aseChecked = true, if it's true, don't output it anymore.

    However, really messy code -- I'd recommend switching to the PDO subsystem for interfacing with the database, and to generate the actual output somewhere else; methods/functions interfacing with the DB should just return values.

  • Well, i do generally do very messy coding.. (self taught and didn't bother to go further) you should see the mess i've got in other scripts i've made it's hilarious..

    and as your first word "easy".. that it was... my brain must be fried lol..
    Thanks heaps @Wintereise

    need to get this finished for tomorrow so i can get it creating reports for customers -.- i dont like my chances lol

  • raindog308raindog308 Administrator, Veteran

    I don't quite understand what you're trying to do - "but only trying to make the "Amount in %" column appear once it appear ONCE in the while loop."

    I suspect you could collapse those three queries to one with a join...that is almost always the case where you do a "SELECT DISTINCT" and then do a foreach...SELECT.

  • 1st suggestion. Use PDO, seriously. My eyes hurt seeing the old mysql_* functions.

    Thanked by 1raindog308
  • AutoSnipeAutoSnipe Member
    edited February 2014

    at the moment, main concern is for it all to actually work properly.. then i can go back and modify it all to make it cleaner..

    and if ya want check out my uber messy coding from 2010

    http://rpiserver.com/lazy.txt

  • @AutoSnipe said:
    at the moment, main concern is for it all to actually work properly.. then i can go back and modify it all to make it cleaner..

    and if ya want check out my uber messy coding from 2010

    http://rpiserver.com/lazy.txt

    my suggestion is start by putting this code up on gist so we can all easily fix it with out sending patches in the old days. i would then follow up with fixing at least the number of sql statments ur having. i think then we will be able to solve the probelm. i dont care if u use old mysql or pdo but just dont set ur self up with a datbase killer.

  • GIANT_CRABGIANT_CRAB Member
    edited February 2014

    Why are you using MySQLel?

    Why are you using PHP?

    Why are you not using Perl?

    Why are you not using Python?

    Why are you not using Ruby?

    Why are you not using PostgreSQLel?

    WHY.

    Thanked by 2Wintereise tchen
  • @wojons
    I might have to look at making a gist account up. As I wouldn't mind clearing up that code (lazy.txt) one day. As I've got one running on an e3 absolutely murdering it 24/7 but it's practically the only thing on it so I'm not fazed

    The current project is just for an internal network site between offices at the financial planner office I work at (to minimise the amount of files we have (approximately 33 4 draw filing cabinets)
    And will make it easier for us to update things if we are out on trips.
    It's also going to be intergrated to a few share buying api's to reduce the load on that too (in the future) but it's coming up to the 6 monthly report so I gotta get the reports out quick otherwise I gotta spend weeks on word doing reports :(

    @giant_crab

    Php and MySQL are the only things I know how to do (half properly) to get the job done at the moment, I'm going to be doing a work funded course to get every other language under the sun done soon too. But that's at another time

  • @GIANT_CRAB said:
    Why are you using MySQLel?

    Why are you using PHP?

    Why are you not using Perl?

    Why are you not using Python?

    Why are you not using Ruby?

    Why are you not using PostgreSQLel?

    WHY.

    Dont bash on language if your gonna help him help other wise just keep quite. None of the other lanagues will make a differance i am sure he is missing a single if statment or something simple not that he needs a crazy query planner like postgresql. and for something basic like this perl python ruby would be the same difference.

    AutoSnipe said: I might have to look at making a gist account up. As I wouldn't mind clearing up that code (lazy.txt) one day. As I've got one running on an e3 absolutely murdering it 24/7 but it's practically the only thing on it so I'm not fazed

    All you need is a github account to use gist. gist.github.com

  • Well when I get back on the computer (on phone ATM) I will check it out :)
    Cheers :))

  • joepie91joepie91 Member, Patron Provider
    edited February 2014

    AutoSnipe said: at the moment, main concern is for it all to actually work properly.. then i can go back and modify it all to make it cleaner..

    Don't do this. It always ends up going the exact same way: the codebase grows and grows and grows, and you'll end up never fixing it.

    You should really be using PDO with parameterized queries from the very start; it's not an upgrade, it's a bare necessity. Your code does not currently meet the minimum bar of quality. mysql_* is deprecated, and for very good reason.

    Also, you should really fix your indentation. Indentation is absolutely vital to writing solid bug-free code, and makes it a whole lot more likely that others will review your code. Similarly, always use descriptive variable names (result instead of q2, for example) for everything that isn't a counter variable.

    To put things in perspective; I'd not even want to look at your code to find the problem right now, simply because it takes me too much (unnecessary) effort to read and understand it, due to the lack of indentation and clear naming.

  • wojons said: Dont bash on language if your gonna help him help other wise just keep quite. None of the other lanagues will make a differance i am sure he is missing a single if statment or something simple not that he needs a crazy query planner like postgresql. and for something basic like this perl python ruby would be the same difference.

    PHP shill alert

    AutoSnipe said: Php and MySQL are the only things I know how to do (half properly) to get the job done at the moment, I'm going to be doing a work funded course to get every other language under the sun done soon too. But that's at another time

    PHP should not be a starting language for anyone.
    C or Assembly should be the starting language.

    Not trying to bash PHP here but...

    The problem is that everyone is using PHP so much so that everyone thinks that its a language for everything.

    Want to write an application in C? Why not just use steal some code from somebody to display a PHP webpage frame?

    I don't even remember the last time I saw a software done in ruby. Other than Discourse for forums, of course.

    MySQL is so outdated because of Oracle acquisition, if everyone still chooses to stick to MySQL instead of switching to PostgreSQL or Firebird, etc, Oracle will continue to NOT update it.

  • @GIANT_CRAB said:
    MySQL is so outdated because of Oracle acquisition, if everyone still chooses to stick to MySQL instead of switching to PostgreSQL or Firebird, etc, Oracle will continue to NOT update it.

    Switching databases is not really a stight forward task for a production system and if the system is already running and runs fine then there is even more reason not to switch it. We have no idea how much stuff this guy has that is in mysql already and if the rest of the platform is stable.

    GIANT_CRAB said: PHP shill alert

    I know PHP i write PHP because i learned it back in highschool when i was working with PHPBB. I have had many jobs that there platforms existed in PHP. I have also had many jobs where i have used python, ruby, go, lua, java and even perl once. People will write code in what ever language they are going to write it in. PHP has evolved a lot since the 4.4.x - 5.2 days. PHP is not the fastest or best language out there but it has its stuff and its playing catch from when it fell behind.

  • @GIANT_CRAB said:
    MySQL is so outdated because of Oracle acquisition, if everyone still chooses to stick to MySQL instead of switching to PostgreSQL or Firebird, etc, Oracle will continue to NOT update it.

    Sorry but what are you even talking about? MySQL is constantly updated and the second most used database management system.

    Latest release: MySQL 5.6.16 - released on 2014-01-31.
    http://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-16.html

  • GIANT_CRABGIANT_CRAB Member
    edited February 2014

    DarioX said: Sorry but what are you even talking about? MySQL is constantly updated and the second most used database management system.

    Latest release: MySQL 5.6.16 - released on 2014-01-31.

    http://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-16.html

    I'm talking about the features here.

    I can update my software everyday but include minor tweaks such as:

    • change software version to 1.02

    • change file name of database.php to databases.php

    You get what I mean.

    PostgreSQL has much better full-text search capability and many more other features.

    Do you know why Fedora and Opensuse replaced MySQL with Mariadb?
    Because Oracle's MySQL is terrible, a fork from the original one by MariaDB is far better with more features as well.

  • vedranvedran Veteran
    edited February 2014

    raindog308 said: I don't quite understand what you're trying to do

    This, but from what I understood

    AutoSnipe said: only trying to make the "Amount in %" column appear once it appear ONCE in the while loop.

    and you have

    AutoSnipe said: if($i == 0) $result .= "*percent Function* %";

    but you set $i to 0 in the loop

    AutoSnipe said: while($s = mysql_fetch_object($q2)) { $i = 0;

    Meaning $i will always be 0 so "percent Function %" is called each time?

  • @GIANT_CRAB said:
    Why are you using MySQLel?

    Why are you using PHP?

    Why are you not using Perl?

    Why are you not using Python?

    Why are you not using Ruby?

    Why are you not using PostgreSQLel?

    WHY.

    Lol. He should be using NoSQL and write it in erlang with twelve queues and a network of pub subs.

  • DarioXDarioX Member
    edited February 2014

    GIANT_CRAB said: I'm talking about the features here.

    Now it makes more sense and I can agree :)

  • hostnoobhostnoob Member
    edited February 2014

    OP, keep using PHP and MySQL. nothing wrong with either. some people just love to jump on the hating PHP bandwagon, even though they never have any valid reasons.

  • raindog308raindog308 Administrator, Veteran

    @GIANT_CRAB said:
    MySQL is so outdated because of Oracle acquisition, if everyone still chooses to stick to MySQL instead of switching to PostgreSQL or Firebird, etc, Oracle will continue to NOT update it.

    You are completely wrong.

    Oracle bought Sun (and MySQL) in April 2009. MySQL was at 5.1 then. Are you saying there have been no new updates since MySQL 5.1? I've seen quite a few new features added in 5.5, 5.6...

    More to the point:

    • I've been to the last three MySQL Connect conferences and talked to MySQL guys. Oracle's strategy is not "let's kill MySQL and make everyone buy Oracle DB". They know that will never happen. Instead, they recognize that MySQL is a separate ecosystem and they run it as yet another business. Oracle has dozens of businesses. Lots of enterprise worlds running Solaris/AIX + Java + Oracle DB. Lots of web/foss/etc. worlds running Linux + {PHP/whatever} + MySQL. They're different worlds.

    • There are more people working on MySQL today than there were when Sun owned it, or when MySQL was a standalone company.

    I, too, think PostGreSQL (not sure what "PostGreSQLel" is) is a better DB, but in the real world we have to look at ecosystem. There's a ton more libraries, frameworks, etc. that support MySQL rather than Pg.

    PHP bashing is stupid. It's my least favorite language and not what I would pick, but for plenty of projects you are part of team, you want/need/must use a particular framework, there is existing code, etc. PHP is not as nice as other languages, but it's certainly in the ballpark and there are good reasons to choose it.

  • If you must use MySQL atleast use percona, it's leaps and bounds over vanilla MySQL.

    Thanked by 1Adduc
  • The guy asked a simple question. 2 people actually tried to help. Others commented with their heads up in their asses.

    Thanked by 2Spirit vedran
  • @serverian said:
    The guy asked a simple question. 2 people actually tried to help. Others commented with their heads up in their asses.

    This.

  • It looks like you're attempting to render the percent of the total in the last column, but you don't have that data ahead of time. You could join against a subquery that sums up the totals, which would technically work. I'd suggest making another query to perform the sum, and calculating the percentage when rendering the view.

    AutoSnipe said: The current project is just for an internal network site between offices at the financial planner office I work at (to minimise the amount of files we have (approximately 33 4 draw filing cabinets) And will make it easier for us to update things if we are out on trips. It's also going to be intergrated to a few share buying api's to reduce the load on that too (in the future) but it's coming up to the 6 monthly report so I gotta get the reports out quick otherwise I gotta spend weeks on word doing reports :(

    As someone who has been brought into a companies to assist in scaling their legacy code for traffic/stability, please please please use best practices now. @joepie91 is right, it's far more likely than not you'll have no time to go back and fix the code.

  • Would it be possible for you to dump your database structure with some data to look at? I can't quite make sense of what you're trying to do, but I bet I could make it a lot easier with better queries - it looks like what you're doing could be solved with some JOINs pretty easily.

Sign In or Register to comment.