PHP - Moved: When To Use Procedural Vs Oop?
This topic has been moved to Miscellaneous.
http://www.phpfreaks.com/forums/index.php?topic=321234.0 Similar TutorialsI am making a space game. The game requires a map. The map needs to contain one or more galaxies. Each galaxy should have from 100 to 1000 star systems. Each star system needs to possess a random number of planets from 1-10. I suspect that I can use while loops to control the number of galaxies star systems and planets, but I need to know how to assign coordinates when making the universe. Hi all, I work on a php project. I wrote it in procedural way. As project grows, it stating be harder to maintain obviously and I want to convert project ( or at lease key features) to oo code. My intention is to replace part which for example connects to database and then use the rest of the script and then move to another part write it in oo and replace old part. I wrote a simple class called Connection, just to handle connection. When I make object it connects, I retrieve some info from database using query as class function. However, there is no way I can execute my procedural login script after I made a connection object. And question is why? Do I need to write whole Database class to handle it? Can I merge oo and procedural php code together in once script? Code sample: Code: [Select] class Connection{ // database access paramiters var $host = 'localhost'; var $data_base = 'database'; var $user = 'user'; var $pass = 'pass'; var $con_handle; // constructing object with automatic connetction to db function __construct(){ $this->connect(); } function connect(){ // open a connection to the database server $this->con_handle = new mysqli ($this->host, $this->user, $this->pass); if (mysqli_connect_errno()) { echo("Could not open connection to database server"); exit; } // selecting database $this->con_handle->select_db($this->data_base) or die(mysql_error()); } } Code: [Select] //{ few includes here} $connection= new Connection(); //{rest of my login script} Folks,
Look what I found he My procedural style code is this alongside their OOP:
<?php if (!$conn) { $error = mysqli_connect_error(); $errno = mysqli_connect_errno(); print "$errno: $error\n"; exit(); } // Get the total number of records from our table "students". $total_pages = $conn->query('SELECT * FROM browsing_histories')->num_rows; //I NEED HELP TO SUBSTITUTE THIS TO PROCEDURAL STYLE // Check if the page number is specified and check if it's a number, if not return the default page number which is 1. $page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1; // Number of results to show on each page. $num_results_on_page = 5; // if ($stmt = $conn->prepare('SELECT * FROM following_histories ORDER BY id LIMIT ?,?')) { if($query = "SELECT id,date_and_time,query_type,followed_word,query_string,browsed_page_original,browsed_page_converted,referral_page_original,referral_page_converted,username,gender,age_range,date_of_birth,skin_complexion,height,weight,sexual_orientation,religion,education,profession,marital_status,working_status,country_of_birth,home_town,home_neighbourhood,home_borough,home_council,home_city,home_county,home_district,home_region,home_state,home_country FROM browsing_histories WHERE username = ? ORDER BY id LIMIT ? OFFSET ?"){ //my substitution of above line $stmt = mysqli_prepare($conn,$query); //MY SUBSTITUTION OF ABOVE LINE // Calculate the page to get the results we need from our table. $calc_page = ($page - 1) * $num_results_on_page; //$stmt->bind_param('ii', $calc_page, $num_results_on_page); mysqli_stmt_bind_param($stmt,'sii',$followee_username,$calc_page,$num_results_on_page); //MY SUBSTITUTION OF ABOVE LINE //$stmt->execute(); mysqli_stmt_execute($stmt); //MY SUBSTITUTION OF ABOVE LINE // Get the results... //$result = $stmt->get_result(); $result = mysqli_stmt_get_result($stmt) //MY SUBSTITUTION OF ABOVE LINE ?> <!DOCTYPE html> <html> <head> <title>PHP & MySQL Pagination by CodeShack</title> <meta charset="utf-8"> <style> html { font-family: Tahoma, Geneva, sans-serif; padding: 20px; background-color: #F8F9F9; } table { border-collapse: collapse; width: 500px; } td, th { padding: 10px; } th { background-color: #54585d; color: #ffffff; font-weight: bold; font-size: 13px; border: 1px solid #54585d; } td { color: #636363; border: 1px solid #dddfe1; } tr { background-color: #f9fafb; } tr:nth-child(odd) { background-color: #ffffff; } .pagination { list-style-type: none; padding: 10px 0; display: inline-flex; justify-content: space-between; box-sizing: border-box; } .pagination li { box-sizing: border-box; padding-right: 10px; } .pagination li a { box-sizing: border-box; background-color: #e2e6e6; padding: 8px; text-decoration: none; font-size: 12px; font-weight: bold; color: #616872; border-radius: 4px; } .pagination li a:hover { background-color: #d4dada; } .pagination .next a, .pagination .prev a { text-transform: uppercase; font-size: 12px; } .pagination .currentpage a { background-color: #518acb; color: #fff; } .pagination .currentpage a:hover { background-color: #518acb; } </style> </head> <body> <table> <tr> <th>Name</th> <th>Age</th> <th>Join Date</th> </tr> <?php while ($row = $result->fetch_assoc()): ?> <tr> <td><?php echo $row['id']; ?></td> <td><?php echo $row['followee_username']; ?></td> <td><?php echo $row['follower_username']; ?></td> </tr> <?php endwhile; ?> </table> <?php if (ceil($total_pages / $num_results_on_page) > 0): ?> <ul class="pagination"> <?php if ($page > 1): ?> <li class="prev"><a href="pagination.php?page=<?php echo $page-1 ?>">Prev</a></li> <?php endif; ?> <?php if ($page > 3): ?> <li class="start"><a href="pagination.php?page=1">1</a></li> <li class="dots">...</li> <?php endif; ?> <?php if ($page-2 > 0): ?><li class="page"><a href="pagination.php?page=<?php echo $page-2 ?>"><?php echo $page-2 ?></a></li><?php endif; ?> <?php if ($page-1 > 0): ?><li class="page"><a href="pagination.php?page=<?php echo $page-1 ?>"><?php echo $page-1 ?></a></li><?php endif; ?> <li class="currentpage"><a href="pagination.php?page=<?php echo $page ?>"><?php echo $page ?></a></li> <?php if ($page+1 < ceil($total_pages / $num_results_on_page)+1): ?><li class="page"><a href="pagination.php?page=<?php echo $page+1 ?>"><?php echo $page+1 ?></a></li><?php endif; ?> <?php if ($page+2 < ceil($total_pages / $num_results_on_page)+1): ?><li class="page"><a href="pagination.php?page=<?php echo $page+2 ?>"><?php echo $page+2 ?></a></li><?php endif; ?> <?php if ($page < ceil($total_pages / $num_results_on_page)-2): ?> <li class="dots">...</li> <li class="end"><a href="pagination.php?page=<?php echo ceil($total_pages / $num_results_on_page) ?>"><?php echo ceil($total_pages / $num_results_on_page) ?></a></li> <?php endif; ?> <?php if ($page < ceil($total_pages / $num_results_on_page)): ?> <li class="next"><a href="pagination.php?page=<?php echo $page+1 ?>">Next</a></li> <?php endif; ?> </ul> <?php endif; ?> </body> </html> <?php //$stmt->close(); mysqli_stmt_close($stmt); //MY SUBSTITUTION OF ABOVE LINE } ?> Edited February 2, 2019 by phpsane I am just wanting to know what the other forms of this type of conditonal would be, the type of if statement is as follows: if ($_POST) {print_r($_POST);} Just seen it a few times now, I know this means if $_POST exists. use the print_r() function to display the entire post array suber global variable. But what are the other combinations other than just if true? Like how are they used like this, like if I wanted to see if a random variable is not identical to another etc. Any helps appreciated, Jez. Hi All, I have the following code which works fine: $mysqli = new mysqli("server", "user", "pass", "database"); if($mysqli->connect_error) { exit('Could not connect'); } $sql = "SELECT customer_contactname, customer_companyname, customer_phonenumber, customer_emailaddress, customer_address1, customer_address2, customer_city, customer_postcode FROM ssm_customer WHERE customer_id = ?"; $stmt = $mysqli->prepare($sql); $stmt->bind_param("s", $_GET['q']); $stmt->execute(); $stmt->store_result(); $stmt->bind_result($ccontname, $ccompname, $cphoneno, $cemail, $cad1, $cad2, $ccity, $cpost); $stmt->fetch(); $stmt->close(); however, when i use my dbconn.php as an include rather than the top section of this code, i get the following error: function prepare() on null The error references the following $stmt = $mysqli->prepare($sql); In my database connection file, i am using the following: $conn = mysqli_connect($db_servername, $db_username, $db_password, $db_name); This seems to be the issue and i believe i am mixing procedural and object based but not sure why there should be a difference here? when i used my database connection file, i do change $mysqli->prepare to $conn->prepare Kind Regards Edited February 4, 2019 by Adamhumbugcreate table mimi (mimiId int(11) not null, mimiBody varchar(255) ); <?php //connecting to database include_once ('conn.php'); $sql ="SELECT mimiId, mimiBody FROM mimi"; $result = mysqli_query($conn, $sql ); $mimi = mysqli_fetch_assoc($result); $mimiId ='<span>No: '.$mimi['mimiId'].'</span>'; $mimiBody ='<p class="leading text-justify">'.$mimi['mimiBody'].'</p>'; ?> //what is next? i want to download pdf or text document after clicking button or link how to do that This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=328845.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=342919.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=328917.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=328753.0 This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=313579.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=319767.0 This topic has been moved to mod_rewrite. http://www.phpfreaks.com/forums/index.php?topic=353027.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=317014.0 This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=305825.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=316254.0 This topic has been moved to mod_rewrite. http://www.phpfreaks.com/forums/index.php?topic=325953.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=342987.0 This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=314397.0 |