Jump to content

spfoonnewb

Members
  • Posts

    276
  • Joined

  • Last visited

Everything posted by spfoonnewb

  1. Your MySQL $db object is not an instance of MySQLi for one thing. Try var_dump(write_mysql_log('Something Happened', $db)); to get the response.
  2. Here is another typical solution for this: SELECT users.username FROM users LEFT JOIN applied_jobs ON users.username = applied_jobs.candidate_name WHERE applied_jobs.candidate_name IS NULL
  3. As an expert, I do agree that in most situations (especially as a newbie to programming) you would want to normalize your data, however there are some cases where it may be better to do it another way. You can point out your opinion while #1 still answering the question and #2 not being so harsh about it. Best programming practices are just that, best practices, but that doesn't mean you should always follow the rule. Every project and application is different, and there are actually legitimate use cases for storing data in such a way. It's the same argument people have about storing serialized arrays or JSON in databases, and I used to think like you. The OP did post that he understands the risks associated with this decision. If he finds out later that he didn't use the best solution, let him learn then, but don't go off telling him you should "never" store data this way, that's simply not true.
  4. There are many ways to do this, however based on the above example, it would be the following: <?php $input = '1234567890024'; preg_match('/(\\d)(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{3})(\\d)/', $input, $match); if (isset($match[0])) { // If index 0 is set it will always match the entire RegExp. unset($match[0]); } $genderArr = array( 1 => 'Male', 2 => 'Female' ); if (isset($genderArr[$match[1]])) echo $genderArr[$match[1]]; var_dump($match);
  5. You can either build your own array to implode (use a key based recursive implode) or build the string yourself. Building the string yourself in this case is easiest assuming you needed that second listeners array rebuilt. Also, you should really validate that IP (make sure it's an IP in the correct format) and/or use prepared statements. <?php //Assume the dataSet appears like this based upon the provided foreach. $returnStats = array( 'listener' => array( (object) array( 'HOSTNAME' => '192.168.1.1', 'USERAGENT' => 'Google Chrome', 'CONNECTTIME' => '60', 'POINTER' => '0', 'UID' => '1Z5548488484' ), (object) array( 'HOSTNAME' => '192.168.1.2', 'USERAGENT' => 'Mozilla Firefox', 'CONNECTTIME' => '120', 'POINTER' => '0', 'UID' => '1Z5584844484' ) ) ); $listeners = array(); $ipStr = 'WHERE IN ('; foreach ($returnStats['listener'] as $listener) { $listeners[] = array( 'HOSTNAME' => (string) $listener->HOSTNAME, 'USERAGENT' => (string) $listener->USERAGENT, 'CONNECTTIME' => (string) $listener->CONNECTTIME, 'POINTER' => (string) $listener->POINTER, 'UID' => (string) $listener->UID ); $ipStr .= "'{$listener->HOSTNAME}',"; } $ipStr = substr($ipStr, 0, -1).')'; var_dump($ipStr); //You can use the listeners array if you need it, but you shouldn't define a key. var_dump($listeners);
  6. You can also use preg_replace('/\s/', '', $input) or explode() and many others. However, are you talking about an array or a string? because you mentioned an array but provided a string.
  7. Assuming the number is always the same length and format, you can use regex. Below is one example: <?php $input = '1234567890024'; preg_match('/([0-9]{1})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{3})([0-9]{1})/', $input, $match); if (isset($match[0]) && $match[0] == $input) unset($match[0]); print_r($match);
  8. Did you follow the first half of the tutorial? It seems as though you are likely missing the required library. The phpinfo() function generates a html page with all the information about the PHP installation, including the information about the Apache web server, the version and settings of the PHP compiler, and the supplemental PHP libraries that are installed and activated. We have to check the pdf libraries. The results should look something like this: PDF Support: enabled PDFlib Gmbh Version: 5.0.3 Revision: $Revision: 1.112.2.11 $
  9. Something like this should work. You would replace 1980 with your set variable. Something like $dob, etc. <!-- Birth Year --> <label for="birthYear">Year Born:</label> <select id="birthYear" name="birthYear"> <option value="">--</option> <?php // Display dates for Users between 18 and 100. for($i = $newestYear; $i >= $oldestYear; $i--) { if ($i == 1980) { echo '<option value="' . $i . '" selected="selected">' . $i . '</option>'; } else { echo '<option value="' . $i . '">' . $i . '</option>'; } } ?> </select>
  10. Here are two examples: <?php $minutes = 10; $time = strtotime('2012-09-10 09:00:00'); $time += 60 * $minutes; echo date('Y-m-d H:i:s', $time); ?> <?php echo date('Y-m-d H:i:s', strtotime('+10 minutes', strtotime('2012-09-10 09:00:00'))); ?>
  11. You can declare the variable static. There several other ways this can be done, however to answer your question in the simplest form, see below: class Main { public $classA, $classB; public static $shared; public function init(){ // Do some stuff return $this; } } class A { public function aMethod(){ $me = Main::$shared; // Same value as from B::bMethod() // Do some stuff return $this; } }
  12. Any text value you feed as the return will be considered true (there are special cases). You should explicitly define the value to be false, or return nothing at all. See: http://php.net/manual/en/language.types.boolean.php If you want the literal (string) value "TRUE," use quotes, otherwise it's boolean.
  13. This isn't really a PHP question. However, please see the following. I believe this is what you are trying to accomplish. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>PhpFreaks Test</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('a.sub').bind("click", function() { $.ajax({ url: 'temp.php', data: 'uid=' + $(this).attr('id'), success: function(response) { $('#test').html(response); } }); return false; }); }); </script> </head> <body> <h3>Test Script</h3> <p> <a href="#" id="1" class="sub">Sub 1</a> <a href="#" id="2" class="sub">Sub 2</a> <a href="#" id="3" class="sub">Sub 3</a> <a href="#" id="4" class="sub">Sub 4</a> <a href="#" id="5" class="sub">Sub 5</a> </p> <h3>Test Section</h3> <p id="test"> </p> </body> </html> <?php #Only because it's self, remove this statement for sub.php if (count($_GET) > 0) { ob_end_clean(); if (isset($_GET['uid'])) { echo $_GET['uid']; } else { echo 'Never use undefined variables. That\'s what that "Notice Error" is.'; } } ?>
  14. Something like this, for example. <?php $result = mysql_query(" SELECT `JobTitle`, `Company`, `Salary`, `Description`, `CompanyURL`, `PhoneNumber`, `Requirements`, `JobCategory`, `JobType`, `Apply_To`, `Email`, `State`, `Address`, `Country`, `Zipcode` FROM `Job-board` ") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { echo 'Job Title: <a href="http://www.destination.com/">'.$row['JobTitle'].'</a><br /><br />'; echo 'Company: '.$row['Company'].'<br /><br />'; echo 'Salary: '.$row['Company'].'<br />'; echo 'Description: '.$row['Company'].'<br /><br /><br />'; } ?>
  15. Try this: <?php $yearList = array(); for ($i = 2010; $i <= 2020; ++$i) { $yearList[$i] = $i; } echo '<pre>'; print_r($yearList); echo '</pre>'; ?>
  16. cURL can definitely do this. Look into CURLOPT_RETURNTRANSFER.
  17. Nothing wrong with your solution, but the key really isn't necessary for the example provided (hmm, pet peeve, maybe.) <?php $submitted = array( array('1', '12113'), array('2', '21200'), array('4', '10200'), array('1', 'yyyy'), array('20', 'xxxxx'), array('5', '21200'), array('7', '21200') ); $finalArr = array(); foreach ($submitted as $dataArr){ $finalArr["{$dataArr['1']}"] = (isset($finalArr["{$dataArr['1']}"])) ? $finalArr["{$dataArr['1']}"] + $dataArr[0] : $dataArr[0]; } ?>
  18. We won't be able to help you with just that. What does your site do? There has to be additional code.
  19. This is basically just XSS. It is client side. The problem is that someone else could send a user a malicious link to your site with potentially whatever code they want. The user will assume that it can be trusted because they trust your site. For example, lets say I use HTML instead of Javascript. I could push your form down, insert my own form, and then post the data to my own page - effectively collecting your users info. As stated above, you need to filter ALL dynamic variables. It doesn't necessarily have to do with the $_SERVER variable, just specific pieces. This is a decent explanation: http://www.owasp.org/index.php/Cross-site_Scripting_(XSS)
  20. I assume this is due to the output not being XML? <?php function createRandomPassword() { $chars = "ABCDEFGHIJKMNOPQRSTUVWXYZ"; srand((double)microtime()*1000000); $i = 0; $pass = '' ; while ($i <= 4) { $num = rand() % 24; $tmp = substr($chars, $num, 1); $pass = $pass . $tmp; $i++; } return $pass; } // Usage $password = createRandomPassword(); $xml = new SimpleXMLElement("<message></message>"); $xml->content = "{$owner} example text - {$password}"; print $xml->asXML(); ?>
  21. I believe what you are looking for is COUNT(IF()). COUNT(IF(Field1 = 1, 0.5, 1)) WHERE Field2 = 'A' This has not been tested, but something like it should work. You may need the SUM() function (instead of COUNT) for what you are looking for. If the field is literally null, IFNULL may be faster.
  22. This should do it: <?php class myclass { public $tempvar=''; public function testA() { $this->tempvar="my new value"; } public function testB() { echo $this->tempvar; } } $class = new myclass; $class->testA(); $class->testB(); ?>
  23. The structure is invalid. <?php function isadmin() { return false; } function ismod() { return false; } if (isadmin($_SESSION['user_id'])) : ?> <br /> It seems that you're an admin. You may <a href="manage_users.php" title="manage users">manage users</a> or <a href="admin_settings.php" title="edit site settings">edit site settings</a>. <? elseif (ismod($_SESSION['user_id'])): ?> <br /> It seems that you're an mod. You may <a href="manage_users.php" title="manage users">manage users</a>. <?php else : echo "none of the above . ."; endif; ?>
×
×
  • 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.