Jump to content

ginerjm

Members
  • Posts

    6,906
  • Joined

  • Last visited

  • Days Won

    99

Everything posted by ginerjm

  1. The purpose of functions is to a) reduce the occurrence of repeating the same code in your script/projects and b) separating out a significant piece of code from the main line of your current process to make the overall code easier to read and follow along with. When you identify any 'task' that you wish to compartmentalize in order to streamline the reading of the entire code assigning a complex (or even simple) set of code to a function and then just calling that code fromt the midst of a process makes great sense. If you have a process to accomplish that involves several distinct and separate steps a good design would be clearly break apart these steps in your code and then create a short piece of code that calls the functions to complete the task in order. For example - to process some input vars, do some calculations, look up some db information and return a result you could break it down into functions that do each of those things. The key is to design your functions to use the appropriate input arguments for each and then pass them from your main process. When you recognize a certain set of code that will come into use many times (or even 2-3 times) in a script/project, make that a function with arguments that allow it to be used with versatility whenever you need that to happen. A simple version might be when you need to compare two values and return some answer that is related to them multiple times. A function that took two input args and returned a value would be both versatile and streamlined and useful all over your project. Experienced programmers are always looking for already-written (and tested) code that does what they want. Good programmers have their own library of functions that they have developed and trust to do what they need. For example - I use a function that I include in all of my database-related scripts to make my db connection and to select the database I wish to use. In it I have included my credentials and as an argument I pass the dbname I wish to have selected and it returns a handle to my connection. Of course I store this script/function outside of my web tree for maximum security.
  2. Yes - this is a very confusing question. "A Constant value that doesn't change"? What a misnomer - a constant is Exactly that - a constant. To ask for "a constant that doesn't change" is simply idiotic. So - as Cronix says - "What are you really looking for?".
  3. How does one give you an example of a database? Let's see - a database is a collection of tables, generally closely related to an application or to a collection of data. It may also contain sets of disjoint data that for one reason or another it makes sense to the designer to lump into a single database. (In a perfect environment (ie, one which you control) this last possibility would be minimized since you would have an unlimited number of databases.) How's that?
  4. It is better that you read up on using databases and how to organize your data fields into the proper table structures (normalization) and then how to write simple queries and try them out. Really - learning it yourself is always better than being spoon-fed and not having a clue why it works.
  5. You hate working with sql? Sounds like a problem to me. If you're going to create appls that use data, the ONLY viable way of managing the data going into the apps is to use sql. Get over your phobia and do a little reading and join the 21st century. Just my $.02
  6. I truly believe my solution is the simpler one. It is concise and clear to someone who may be new to php and this kind of processing. What's the objection to having a second fetch in the loop? And no, it is not designed to handle 3 column tables or more - it simply works for the problem outlined and works simply.
  7. Chocu3r: I don[t know what code you are seeing but I see a loop that fetches one row, moves array values into local vars, begins a table and row and td element and then outputs name, link, author and cover two times and then closes the table. Then it loops and gets the second query result row and repeats. End result - a table for each result row containing two images side by side of the same data. The if statement you refer to does not exist in OP's code. I stand by my original post, which I have used in the past. Here is my version of OP's code: // Display Content echo "<table>"; while ($rows = mysqli_fetch_array($result)) { echo "<tr>"; // output current row WriteResult($rows); // get a second result row and output it if ($rows = mysqli_fetch_array($result)) { WriteResult($rows); } else echo "<td> </td>"; // complete the current row echo "</tr>"; } echo "</table>"; //********************** function WriteResult($rows) { $Name = $rows['Name']; $Author = $rows['Author']; $Cover = $rows['Link to Cover']; $Link = $rows['Link to Profile']; echo "<td>"; echo "<a href='$Link'>$Name</a>"; echo "<br />$Author<br />"; echo "<a href='$Link'><img src='$Cover' /></a>"; echo "</td>"; return; }
  8. I may be having a bad day but 'mysqli_fetch_array' fetches ONE row of data stored in array format. No? Where is the 2nd row coming from to complete the first table row? The OP's original logic is clearly flawed and my comments should help him see that, as well as give him a method to get two rows of data in each pass thru his loop.
  9. ginerjm

    multi query

    1 - Stop using MySQL_* functions. They are soon to be defunct. Check the manual for proof and then research mysqlI or PDO for your db acitviity. 2 - You have a delele query already. Only problem is you need to sanitize your input argument to be sure it is valid and does not contain any malicious data in it. Or - when you switch to PDO (my preference) use a prepared query statement instead. 3 - How are you going to get the data from the deleted record to insert into the other table? Personally, I would do a query to read the 'old' record, insert that data into the new table, and then go back and do the delete of the old record. (Of course there is probably a cleaner more efficient way to accomplish this but since I've never had to do this, I haven't researched this.)
  10. Add some echoes to see where you logic is actually taking you to debug this.
  11. ?? Pravins solution utilizes an array. Of course he doesn't give the OP any guidance on creating an array that fits his situation, but yes, it can easily be done. My solution simply uses what is at hand and avoids that.
  12. My solution works without loading an array and was addressed to the OP, not PravinS
  13. You need to fetch a second row in the middle of your loop. Currently you create a row, insert a td element and 3 values from the first row. After that you need to do another fetch (while still inside your loop) and insert a 2nd td and the fields from the new row, THEN end that row and let the loop repeat. Of course if the second fetch fails (you need to check that), you need to output an empty td element and still close the row. After the loop you close the table (you only need one table element)
  14. Sorry - but since I don't understand what "shorten links" means at all, and since your code doesn't help me to understand, I can't offer any help.
  15. Yes you do - so why did you state just the opposite in your item #1? Writing chunks of html code and populating it with php vars is the way to go. Leave the php logic out of the end result. Your ex. of the two syntaxes is meaningless since you can achieve the same outcome in either syntax without an echo. It's just that the alt. syntax is so rarely seen and therefore rarely used. Of course it may have had more usage years ago - I wasn't around then. But again - your example is a poor one if you truly intend to separate your php code and html code. It sounds like your attempt to pre-plan your standards for your upcoming large project may be poorly thought out. Without significant experience in writing some heavy duty code you will surely end up making mistakes and using unsound practices. Not a criticism - merely preparing you for the day you look thru your efforts and (with increased awareness) discover that you didn't fully understand things back then. If this large project is something you are doing on your own, I suggest that you continue to grow on some smaller stuff first. Having only been using php for a little over 3 years I marvel at some of my earliest scripts (just simple single modules) and the way I wrote things back then. Whatever was I thinking??
  16. Sorry - I don't click on posted links. If you can't show the code, oh well.....
  17. 'shorten links' ? That means less to me than shrinking links. What are you talking about? An apache re-write rule perhaps?
  18. You can't embed php code inside a js function. You need to let your js function return true/false to your submit button and that will determine if your submit calls the php script that will do the update <input name = "btn_seatOccupied" type = "submit" class="btnFORM" value="CONFIRM" onClick="return seatOccupied()" action='postdata.php'> Note the addition of 'return' - this will either submit the form and call the script to do the update for you. Or not.
  19. I have problems with this statement: I am looking to use the alternative syntax for control structures as to make the php code inside my html readable and easier to manage but in terms of OOP when is it best for me to use classes vs functions? a) you should not be considering how your php code looks inside your html code. It should not even Be There!! b) and whatever does 'alternative syntax' have to do with design considerations? IMHO - using the alternative syntax is a surefire method of confusing the next person who looks at this code. What's wrong with the 'recommended' or 'non-alternative' syntax? Why do you want to use the one that few use? (Isn't that why it's called alternative?)
  20. Your last post is just fine. If you the file does not exist where you think it should be, you'd get an error message from your browser. Period. Unless you have some kind of apache re-write rule in place(?). More likely is that this exact piece of code is NOT what you are executing on your client. Display the source of your web page before clicking the link and ensure it is the current file.
  21. Does this script belong to a CMS package?
  22. 1 - you say you're familiar with css - why don't you build a css file and include THAT so you don't have ALL THAT MESS of CSS in your html? 2 - you say over and over how your includes aren't working yet the code you posted does not have a single include (or require) in it. How are we supposed to help you?
  23. Let's see. You have a head tag, then you include some code, then you call a function from that include which re-issues a head tag, close the head, and then you issue a second end head tag. AND you open and close your html inside of the header section. I would think that any browser would have a huge problem handling this mess. BTW - why a function? You're including static html code - why make it a php function?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.