Jump to content

Solar

Members
  • Posts

    145
  • Joined

  • Last visited

Posts posted by Solar

  1. Hey, I'm stumped. Through out this post, I will bold my questions.

    Is global $pdo; frowned upon? is it safe? is there a better way to access it?

    As of right now, I'm using the easy way of creating a database php file and including it in specific php pages to access the $pdo variable but I would like a different approach. I would like to use a database class.

    Let's say I have my class/class_database.php -- converted from a non-class.

    <?php
    	class Database{
    		private $pdo;
          
    		function __construct(){
    			try{
    				$pdo = new PDO('mysql:host=HOST;dbname=DBNAME', "USER", "PASSWORD");
    				$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    			}catch(PDOException $e){
    				echo 'Connection failed: '.$e->getMessage();
    			} 
    		}
    	}
    ?>

    I would like to create a separate class called Statements:

    class Statements{
    	function select($sql,$arr){
    		$stmt = $this->pdo->prepare($sql);
    		if(empty($arr) == false){
    			foreach($arr as $k => $v){
    				$stmt->bindValue($k, $v);
    			}
    		}
    		$stmt->execute();
    		return $stmt->fetchAll(PDO::FETCH_ASSOC);
    	}
    }

    The problem I'm having is that I would have to use extends in order to access the private pdo:

    class Statements extends Database

    Is extending a Database class safe or recommended? If not, how can I use a construct inside of Statements to obtain the Database Class PDO variable? Because every time Statements is called, it will need to use the pdo from the Database Class.

    class Statements{
    	private $pdo;
    		
    	function __construct(Database $newPDO){
    		$this->pdo = $newPDO;
    	}
    
    	//functions below...
    }

    How exactly do I pass the pdo to the __construct function and make the call? I would easily assume that creating a get function inside of the Database Class to get the private $pdo variable is risky business.

    After doing a bunch of research online, it seems like programmers fight over global pdo variables and extends of the Database because it logically doesn't make sense. Unfortunately, I have yet to find a reasonable answer. I appreciate your time!

  2. 6 minutes ago, ginerjm said:

    Return the vars as arguments of the function with the & on each.

    "function xyz(&$a, &$b)"

    Call the function with "function ($a, $b)"

    and then use $a & $b in your following code.

    As if it's that simple! Thanks a ton!
    Edit: I guess either way, I still have to specify the variables. I'm good with this!

     

    $account = new Account();
    $account->getUserInfo($email,$rank);

     

    class Account{
    	function getUserInfo(&$email,&$rank){
    		global $con;
    		$stmt = $con->prepare('SELECT email, rank FROM accounts WHERE id = ?');
    		$stmt->bind_param('i', $_SESSION['id']);
    		$stmt->execute();
    		$stmt->bind_result($email, $rank);
    		$stmt->fetch();
    		$stmt->close();
    	}
    }

     

  3. Let's say I have a simple statement.

    $stmt = $con->prepare('SELECT email,rank FROM accounts WHERE id = ?');
    $stmt->bind_param('i', $_SESSION['id']);
    $stmt->execute();
    $stmt->bind_result($email,$rank);
    $stmt->fetch();
    $stmt->close();

    I can then echo the e-mail and rank variables very easily in PHP

    <?=$email?> <?=$rank?>

     

    What is the best way to echo the results with a bind_result while it's inside of a function?

    function getUserInfo(){
    	global $con;
    	$stmt = $con->prepare('SELECT email,rank FROM accounts WHERE id = ?');
    	$stmt->bind_param('i', $_SESSION['id']);
    	$stmt->execute();
    	$stmt->bind_result($email,$rank);
    	$stmt->fetch();
    	$stmt->close();
    }

    I could easily stick email and rank into an array and return it but that's duplicating code. Is there a way to fetch the bind_result and return all variables inside of it? I greatly appreciate it 🙂

  4. Hey PHPFreaks!

    I've ran into a problem and hoping you could help me out. I did not design these tables and I am having problems enforcing referential integrity.
    Is a constraint required for this task?

    Here are the two tables

    ClientsTable (ClientID, ClientName)

    VALUES("11111TOR","JOE");
    VALUES("22222OTT","BOB");
    VALUES("33333NEW","PAM");

     

    TimezoneTable (Location, Timezone)
    VALUES("OTT", "EST")
    VALUES("TOR", "EST")
    VALUES("CAL", "MDI")
    VALUES("NEW","EST")

    There is a one to many relationship between TimezoneTable and ClientsTable. Due to the data, I cannot enforce referential integrity.

    What is a solution in joining these together? Could I do so by creating a new table? How could I go from there?

    Thanks in advanced,
    Solar

  5. I have decided to take a different approach to this method. After researching; everyone's reply is the same response I recieved when reviewing the PHP.net website.

    Thank-you for your time, if the method was possible, it would have saved me a ton of time. But in the long run, the method I expected is not proper.

     

    Thanks again,

    Solar

  6. I understand the $_GET[''] function.. but I have ran into an error.

    Lets say my URL is localhost.php/test.php?say=#hello

    The $_GET[''] function will only take the print after the equal and before the pound sign. - Which is blank

     

    My question is... What function should I use to parse after the # sign?

     

    Thanks,

    Steven

  7. Well, I never realized this but when submitting a form, either Quotation Marks or Apostrophes works. I maybe "bad coding" it.

     

    To have Quotation Marks work

    <?php mysql_query("INSERT INTO wallposts (username, userfriend, date, ip, message)VALUES(\"$username\", \"$userfriend\", \"$date\", \"$ip\", \"$message\")"); ?>

     

    To have Apostrophes work

    <?php mysql_query("INSERT INTO wallposts (username, userfriend, date, ip, message)VALUES('$username', '$userfriend', '$date', '$ip', '$message')");  ?>

     

    I mainly need both to work for $message. Is there a simple solution? Do I need to extend a variable?

     

    Thanks!

     

    Edit: Extending my question for a better understanding.

    When a user uses characters like quotation marks and or apostrophes one or the other works, depending on the code provided above. How can I fix this?

  8. I was able to find some old files.

     

    /******  build the pagination links ******/  
    // range of num links to show   
    $range = 3;   
      
    // if not on page 1, don't show back links   
    if ($currentpage > 1) {   
       // show << link to go back to page 1   
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";   
       // get previous page num   
       $prevpage = $currentpage - 1;   
       // show < link to go back to 1 page   
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";   
    } // end if    
      
    // loop to show links to range of pages around current page   
    for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {   
       // if it's a valid page number...   
       if (($x > 0) && ($x <= $totalpages)) {   
          // if we're on current page...   
          if ($x == $currentpage) {   
             // 'highlight' it but don't make a link   
             echo " [<b>$x</b>] ";   
          // if not current page...   
          } else {   
             // make it a link   
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";   
          } // end else   
       } // end if    
    } // end for   
                
    // if not on last page, show forward and last page links       
    if ($currentpage != $totalpages) {   
       // get next page   
       $nextpage = $currentpage + 1;   
        // echo forward link for next page    
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";   
       // echo forward link for lastpage   
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";   
    } // end if   
    /****** end build pagination links ******/  

     

    I hope the issue gets fixed soon!

    Thanks!

  9. We have encountered an error that prevents us from showing this page. Please try again later. If the problem persists, please contact an administrator.

     

    Would anyone kindly have the bottom half of the script? or the whole script?

    I thought I had it saved somewhere... Bottom half meaning the pagination part. Where it echos first, previous, current, next, last hyperlinks.

  10. Hey! I am attempting to create a fun game by using some of the communities code!

     

    The game is Higher, Lower, or the Same.

    I have modified the code to use a "Same" option but I just realized that it is useless unless I fix the problem.

     

    I want to be able to have 52 cards. These cards should not be doubles. For example; Your first card is a jack of clubs, I don't want that jack of clubs to show up again until after all 52 Cards are played. Then I want the deck to reshuffle so users can continue to play. Please tell me if I need to add more detail, I think this is understandable.

     

    Here is the source -

    <?php session_start(); ?>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <?php
    function deal (&$pack, $numhands, $cards_per_hand) {
    $hands = array();
    if ($numhands*$cards_per_hand > 52) return $hands;
    $temp = array_slice ($pack, 0, $numhands*$cards_per_hand);
    $hands = array_chunk($temp, $cards_per_hand);
    return $hands;
    }
    
    $vals = array('1','2','3','4','5','6','7','8','9','10','j','q','k');
    $suits = array('c','d','h','s');
    
    $pack = array();
    
    foreach ($vals as $v) {
    foreach ($suits as $s) {
    $pack[] = "$s$v";
    $value[] = "$v";
    $suit[] = "$s";
    }
    }
    
    $card_picked = array_rand($pack);
    $card_number = $value[$card_picked];
    echo "<img src=\"banner.png\">";
    
    if (isset($_GET['sub']))
    {
    switch($_GET['sub'])
    {
    case 'Higher':
    echo $card_picked > $_SESSION['card'] ? '<h1>Correct</h1>' : '<h1>Wrong</h1>';
    break;
    case 'Lower':
    echo $card_picked < $_SESSION['card'] ? '<h1>Correct</h1>' : '<h1>Wrong</h1>';
    break;
    case 'Same':
    echo $card_number == $_SESSION['number'] ? '<h1>Correct</h1>' : '<h1>Wrong</h1>';
    break;
    
    }
    }
    $show_card = $pack[$card_picked];
    $card_number = $value[$card_picked];
    $card_suits = $suit[$card_picked];
    $_SESSION['card'] = $card_picked;
    $_SESSION['number'] = $value[$card_picked];
    ?>
    <form>
    <?php echo "<img src=\"cards/".$show_card.".gif\">"?><br><input type="submit" class="button" name="sub" value="Higher"> <input type="submit" class="button" name="sub" value="Lower"> <input type="submit" class="button" name="sub" value="Same">
    </form>
    <?php 
    //echo "$card_picked";
    //echo "$card_number";
    //echo "$card_suits";
    ?>

     

    I was thinking would it be best to array the cards from a mysql table?

    To repeat my main question: How can I make the cards not double until the deck (52 cards) have been played, then re-shuffled?

     

    Thanks!

  11. I snagged before you had edit your post.

     

    $catIDs = array(  
    3 => "Automotive",
    4 => "Business",
    5 => "Careers",
    6 => "Education",
    7 => "Financial",
    8 => "Government",
    9 => "Health",
    10 => "Mobile",
    11 => "Organization",
    12 => "Programming",
    13 => "Software",
    14 => "Travel",
    15 => "Web",
    16 => "Other",
    17 => "Gaming"
    );
    $catid = array_search(trim($_POST['catid']), $catIDs);

     

    This works WAY better. Cleaner, and nicer. This will open up more solutions around my website! Thankyou very much!

  12. Thanks for the fast replies! I figured it out using the manual way. No matter how many times I try doing arrays... They never work for me. My form selection is using Automotive, Business, Careers, etc... So I want them to transfer to numbers so when inserted with mysql, they display correctly in the blog id categories.

     

    <?php if($_POST['catid']=="Automotive"){ 
    $catid = '3';
    }
    if($_POST['catid']=="Business"){ 
    $catid = '4';
    }
    if($_POST['catid']=="Careers"){ 
    $catid = '5';
    }
    if($_POST['catid']=="Education"){ 
    $catid = '6';
    }
    if($_POST['catid']=="Financial"){ 
    $catid = '7';
    }
    if($_POST['catid']=="Government"){ 
    $catid = '8';
    }
    if($_POST['catid']=="Health"){ 
    $catid = '9';
    }
    if($_POST['catid']=="Mobile"){ 
    $catid = '10';
    }
    if($_POST['catid']=="Organization"){ 
    $catid = '11';
    }
    if($_POST['catid']=="Programming"){ 
    $catid = '12';
    }
    if($_POST['catid']=="Software"){ 
    $catid = '13';
    }
    if($_POST['catid']=="Travel"){ 
    $catid = '14';
    }
    if($_POST['catid']=="Web"){ 
    $catid = '15';
    }
    if($_POST['catid']=="Other"){ 
    $catid = '16';
    }
    if($_POST['catid']=="Gaming"){ 
    $catid = '17';
    } ?>

     

    If I were to do the array.... Would I want that backwards?

    "Automotive" => 3,

    "Business" => 4,

    etc...

    Or the way you have it is fine? I get an error.

    Notice: Undefined index: catid

     

    Warning: array_search() expects at least 2 parameters, 1 given

  13. What is the most effect approach to doing this?

     

    <?php
    $catid = $_POST['catid'];
    
    if($catid = "Automotive"){ 
    $catid = '3';
    }
    if($catid = "Business"){ 
    $catid = '4';
    }
    if($catid = "Careers"){ 
    $catid = '5';
    }
    if($catid = "Education"){ 
    $catid = '6';
    }
    if($catid = "Financial"){ 
    $catid = '7';
    }
    if($catid = "Government"){ 
    $catid = '8';
    }
    if($catid = "Health"){ 
    $catid = '9';
    }
    if($catid = "Mobile"){ 
    $catid = '10';
    }
    if($catid = "Organization"){ 
    $catid = '11';
    }
    if($catid = "Programming"){ 
    $catid = '12';
    }
    if($catid = "Software"){ 
    $catid = '13';
    }
    if($catid = "Travel"){ 
    $catid = '14';
    }
    if($catid = "Web"){ 
    $catid = '15';
    }
    if($catid = "Other"){ 
    $catid = '16';
    }
    if($catid = "Gaming"){ 
    $catid = '17';
    } ?>

     

    I tried this and it works;

    <?php
    $catid = "Automotive";
    if($catid = "Automotive"){ 
    $catid = '3';
    }
    echo $catid;
    ?>

     

    But... When I add another if statement...

    <?php
    $catid = "Automotive";
    if($catid = "Automotive"){ 
    $catid = '3';
    }
    if($catid = "Gaming"){ 
    $catid = '17';
    }
    echo $catid;
    ?>

     

    It echos $catid as 17 and not 3 which is defined as a variable.

     

    I hope there is a simple solution for this!

    Thanks in advanced!

  14. Hello! I've got exactly what I've wanted in my previous topic so I have decided to make a new topic for some help.

     

    General

    General Discussion

    Description here

     

    Entertainment Discussion

    Description here

     

    Gaming

    Console Discussion

    Description here

     

    Handheld Discussion

    Description here

     

    And a database like this;

    id title description parent

    1 General description here 0

    2 Gaming description here 0

    3 General Discussion description here 1

    4 Entertainment Discussion description here 1

    5 Console Discussion description here 2

    6 Handheld Discussion description here 2

     

    <?php
    function generate_forums($parent)
    {
    $has_childs = false;
    
    global $forums_array;        
    
    foreach($forums_array as $key => $value)        
    {
    if ($value['parent'] == $parent)
    {
    if ($has_childs === true)
    	{
    	$has_childs = false;
    	}
    	echo '<a href="/category/' . $value['title'] . '/">' . $value['title'] . '</a><br>' . $value['description'] . '';        
    	generate_forums($key);        
    	echo '<br>';
    	}
    } 
    }
    generate_forums(0);
    ?>

     

    The code works wonders. But it is hard to customize template wise.

     

    $ID#1 $title = General

    I would start off with <table>

    I need to have the Categories that Parent = ( 0 ) to have a HTML <tr>

    I also need the Categories with Parent = ( Value ) to have a HTML <td>

    Then the tags would be closed off before $ID#2 $title = Gaming's template would start again.

     

    I keep trying to scramble the coding around and can't seem to find that probably "simple" touch.

    Thanks in Advanced.

  15. This makes perfect sense; I never thought of it that way! Thank you very much for your time.

     

    Threads and post will go in a different database with their own unique ID as well!

  16. Oooh, Sorry about that. yes. The "Cat" column my first post is the main Categories.

     

    My Sub Forums database has a cat column as well. So the forums showed in the main Category.

     

    But you answering: I believe I have the idea.

     

    Keep Main Categories and Sub Categories all in the same database? Is that a good idea to do?

    I think that is an easy route to take.

     

    If not; any suggestions?

  17. Lets say I have a simple table for Categories;

    fidforumdesccat

    1General Forumdesc here1

    2Help Forumdesc here2

    3Console Forumdesc here3

     

     

     

    Hello! I think I need some creative juice to start my flow and am thankful for any suggestions.

     

    I am looking into some advice on how to code in "Category" wise.

     

    Lets say if I have forums like this;

     

    General Forum

    General

    Talk about anything in general to NewbWorld.

     

    Suggestions

    Have any ideas to give to NewbWorld?

     

    Anything Goes

    Talk about anything you want!

     

    Help Forum

     

    HTML Help

    If you are having problems with HTML coding post here!

     

    PHP Help

    If you are having problems with PHP coding post here!

     

    Instead of me manually coding each Category (General Forum, Help Forum, Console Forum, etc)... I want to be able to have the following coded like so;

     

    List Category

    ---Forum Sub Category

    ---Forum Sub Category

    ---Forum Sub Category

     

    List Category 2

    ---Forum Sub Category

    ---Forum Sub Category

    ---Forum Sub Category

     

    Right now; I've been coding the "List Category" manually to show "Sub Forums". I would like to know how to do this without manual anymore. I would love to start coding an admin/moderator panel soon.

     

    Any help is much appreciated and thankyou for your time!

×
×
  • 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.