![](https://forums.phpfreaks.com/uploads/set_resources_1/84c1e40ea0e759e3f1505eb1788ddf3c_pattern.png)
keldorn
Members-
Posts
340 -
Joined
-
Last visited
Never
Everything posted by keldorn
-
[SOLVED] General question about proper coding...
keldorn replied to galvin's topic in PHP Coding Help
I use Smarty for all my projects to achieve separating my code. To be honest I didn't like Smarty when I first starting learning programming. I thought it was like a cryptic language to learn. But I think now people who say that are just noobies to begin with. Its really friggen easy to use. -
Thats the most godawful code I have seen for a contact form. There is not even any validation. I could send an email with html tags and unlimited characters for example. Is there even captcha on it? Lastly using echo "error" for any error is really just bad. The variables they did fill out should be returned to the form along with the error details. Thats just awful. I would consider finding a new programmer for hire and I wouldn't use that script. (well techically I would just make my own. )
-
[SOLVED] General question about proper coding...
keldorn replied to galvin's topic in PHP Coding Help
My view on proper coding is to seperate the code from the template as much as possible. -
I think i would do something like this. Consider this logic. Create a table in your mysql database for a time, say "last activity", set a time in session also $_SESSION['last_activity'] and evalute it on each page load, if it goes over 10 minutes from last activity time, then reupdate the session time, and update their coins by 10 coins. (1 coin x 10 minutes). Also reupdate their database field saying there last time.. when thye log in again when they dont have their session. Then evaluate the time in the database of their activity and compare is too the current time and then evaluate how many coins. Good luck.
-
Problem solved. For some reason this was cuasing the bug. $sorted[$country] = array("<a href=\"url.php?id={$link['id']}\">{$link['short_url']}</a><br/>"); $sorted[$country] = array('country'=>$country); <-- Point of bug Changed to this. $sorted[$country] = array('country'=>$country,"<a href=\"url.php?id={$link['id']}\">{$link['short_url']}</a><br/>"); I'm guessing what was happening, was when it hit. $sorted[$country] = array('country'=>$country); It unset the array index for that country, and then when it looped again (if). Then it skipped that and fill out the index normally. btw I notice you guys never reply to threads that invole complicated multidemisional arrays. Are they too complicated for you guys? LOL
-
I'm generating this array by giving this code below a Mysql Result from mysql_fetch_assoc. Mysql Returns a result that looks like this. [159] => Array ( [id] => 3203 [country] => DE [short_url] => example.net ) [160] => Array ( [id] => 3204 [country] => US [short_url] => example.com ) This is my code: $cw = array('ad'=>'Andorra', 'ae'=>'United Arab Emirates', 'af'=>'Afghanistan', 'ag'=>'Antigua and Barbuda', 'ai'=>'Anguilla', 'al'=>'Albania', 'am'=>'Armenia', 'an'=>'Netherlands Antilles', 'ao'=>'Angola', 'aq'=>'Antartica', 'ar'=>'Argentina', 'as'=>'American Samoa', 'at'=>'Austria', 'aw'=>'Aruba', 'ax'=>'Aland Islands', 'az'=>'Azerbaijan', 'ba'=>'Bosnia and Herzegovina', 'bb'=>'Barbados', 'bd'=>'Bangladesh', 'be'=>'Belgium', 'bf'=>'Burkina Faso', 'bg'=>'Bulgaria', [.... and continues threw all the country codes...]); // Contains Array of countries, 2 letter code => Full name. $cw2 = array_flip($cw); $sorted = array(); foreach($result as $link){ $country = strtolower($link['country']); if(in_array($country,$cw2)){ if(array_key_exists($country,$sorted)){ $arr[$country] = array("<a href=\"url.php?id={$link['id']}\">{$link['short_url']}</a><br/>"); $sorted[$country] = array_merge_recursive($arr[$country],$sorted[$country]); } else { $sorted[$country] = array("<a href=\"url.php?id={$link['id']}\">{$link['short_url']}</a><br/>"); $sorted[$country] = array('country'=>$country); } } } This will generate an array looks like this. Array ( [us] => Array ( [0] => <a href="url.php?id=1">somelink</a><br/> [1] => <a href="url.php?id=3">somelink</a><br/> [country] => us ) [ca] => Array ( [0] => <a href="url.php?id=1">somelink</a><br/> [1] => <a href="url.php?id=3">somelink</a><br/> [country] => ca ) [de] => Array ( [0] => <a href="url.php?id=1">somelink</a><br/> [1] => <a href="url.php?id=3">somelink</a><br/> [country] => de ) However, I noticed that this is omitting 1 value for each country index! For example in the Mysql array thats returned say I have, 2 results that have the country code "us", You think this would be generated from the code above. Array ( [us] => Array ( [0] => <a href="url.php?id=1">somelink</a><br/> [1] => <a href="url.php?id=3">somelink</a><br/> [country] => us ) But that is not what is happening. This is what is happening Array ( [us] => Array ( [0] => <a href="url.php?id=1">somelink</a><br/> [country] => us ) Lastly if there is one value returned from mysql with that country code I get this. [de] => Array ( [country] => de ) Completly blank! But there was 1 result, it should of put it in there right? Can anyone see the bug in the code above thats doing this? Please someone! lol
-
I use classes to make something "important" so I dont have to reinvent the wheel everytime I need that functionality. Like pagination for example.
-
OOP was a hard concept for me grasp too at first. about 20 minutes in the video. Where he shows this tree model of "animals". Then it hit me, Imagine this. $bob = new $human('male'); $viconia = new $human('female'); THat would generate 2 object with an array tha tcontains all the information about hair color, body size, skin color and everything perhaps randomly generated. These objects of class "human" would extend mammal, mammals would extend animals, and animals would extend matter, and matter would extend physical laws and gravity. Any number of class could branch off certain parts and have private variables. For example you would not beable to access a variable from a bird class that extend off animal from human becuase its protected. Also on the bottom class of material laws. (matter) human would inherit to be flammable while the class "rock" that extends from it wouldn't. The whole world really made up in way, yes sorry you are all objects, so welcome to the matrix!! LOL In the perfect matrix there is no layer of "procedural" code that overlays this. Its all one object that hums together. Sorry if that sounds ridicolous. I have an extreme visual imagination. I still haven't found any PHP code that does that yet. lol But if it where this 1 line would start your whole application. // After programming for 7 days, God said: $start = new start; echo "welcome to the matrix";
-
Dont you just love when your answer hits you like ton of bricks? foreach($array as $tmp){ $color = $tmp[status]; echo "{$color_array[$color]}"; }
-
Say I have this array for example: print_r($array); Prints: Array ( [0] => Array ( [status] => 0 ) [1] => Array ( [status] => 1 ) [1] => Array ( [status] => 0 ) ) Now I have another array: $color_array = array('0'=>'nocolor','1'=>'green'); How I can do the logic of this? foreach($array as $tmp){ // Say status holds "1" in this case so it translate to //$color_array[1]; = green echo "{$color_array[$tmp['status']]}"; } Obviously the above generate Illegal offset type. So I cant do that.
-
I was validating a form input I tried first seeing If I could do an SQL injection with no validation. This was how my query was set up. $var = $_POST['input']; ...mysql_query("UPDATE table SET table='{$var}' WHERE.... I tried inputing things like x" WHERE and """"""" ''''' '''"""etc.. int the form and it had no mysql errors. It just inserted it into the database like that. So does this have something to do with Magic quotes?
-
By the way if you banking on for SEO (search engine optimization) using my method above would be undesirable. As Google, Bing, Yahoo etc would include "?redirect" on your site.... Second method you could use is their HTTP referer (via $_SERVER['HTTP_REFERER'] ) and see if it set and matches your domain 1. However that is 100% reliable either as not everyone has it set.
-
hmm, You could include in the redirect a $_GET variable. When "Domain 1" redirects to "Domain 2" Have this in the url.. domain2.com/?redirect Then in domain 2 have if(isset($_GET['redirect'])){ $msg = "Hello you have been redirected!"; }
-
How to access $config['var'] variable in Class.
keldorn replied to keldorn's topic in PHP Coding Help
Yes that works, I was reading however that method is the "undesirable" way to do it. (I could also include the config.php file in the __construct() to achieve that same thing I believe which would be even more undesirable). Except they forgot to mention the "proper" way or why its the least preferred method. haha I'm only left with unanswered questions. If anyone could fill in why using global inside a class is undesirable and what is the better method that would great. I guess for now I'll use global inside the __construct. keldorn -
I'm more in procedural, anyways I am trying out OOP. I have this class that does Mysql Database stuff. But I hit a wall with this. I have variables like $config['database_pass'] = "password" in a config.php file. I can't access it inside this class. So how is done properly? I keep getting errors. Like "unexpected T_VARIABLE" class database{ public function __construct(){ /* Connect to mysql (Area of my problem)*/ $sql = array(); $link = mysql_connect($CONFIG['database_host'], $CONFIG['database_user'], $CONFIG['database_pass']) or die('Could not connect: ' . mysql_error()); $db_selected = mysql_select_db($CONFIG['database_database'],$link) or die(mysql_error()); } // Return array of categories public function get_cat(){ $sql = mysql_query("SELECT * FROM category") or die(mysql_error() . " In category.php file in class database function get_cat"); while($line = mysql_fetch_assoc($sql)) { $tmp[] = $line; } if(is_array($tmp)){ return $tmp; } else { return false; } } }
-
I need help understanding Multidimensional Arrays in Smarty with Mysql
keldorn replied to keldorn's topic in PHP Coding Help
OK, I tried this. {section name=post loop=$article} <span class="meta">Published by: {$article[post].article_author} in Category {$category[post].category_name} </div> {sectionelse} Nothing Found! {/section} It puts on the the page Published by: Keldorn in Category PHP buts its still wrong becuase the article_Category for that one was 2 not 1, so it should say Javascript. This is becuase its using the Incrementing ID of the array to match the category_name , not the article_catogory field (!) I tried this. {$category[article_category].category_name} But get Undefinded index: article_category. -
This is the frist time I've had to deal with this, I belive its called multidimensional arrays? I have 2 arrays after pulling them from Mysql (This have been chopped down to remove unneeded information/logic.) $Category = Array ( [0] => Array ( [id] => 1 [category_name] => php [slug] => PHP ) [1] => Array ( [id] => 2 [category_name] => javascript [slug] => Javascript ) ) $article = Array ( [id] => 1 [article_content] => Welcome this is your first article, use the admin panel to edit or delete this. [article_category] => 1 [article_author] => Keldorn ) ) I need to take the [article_category] from $article and match it too [id] of $category and the get the corresponding [category_name] Like if the article_category => 1 then the it should print "PHP" from the category array. If someone could show me how to do this with Smarty snytax that would be great! This is what I have so far. {section name=post loop=$article} <span class="meta">Published by: {$article[post].article_author} in Category (ie. PHP)<!-- need a multidimensional array here --></div> {sectionelse} Nothing Found! {/section} Keldorn
-
what I mean, is that Google, yahoo etc will crawl that link think its a new page? err well duplicate page.
-
The issue is that it creates duplicate pages. A search will treat that link as a new page. http://www.php.net/downloads.php/a/fake/link/here/ http://www.youtube.com/index.php/look/a/fake/link That should return 404 not found.
-
I noticed on my site, I can take pages and put for example index.php/test/does/not/exist/ and it will return the index.php as if this were another page with 200 OK response and leave the slashes on there as if it were a directory. I dont know why that is working that way. I am not using path info urls nor do I have any .htacces rules Also I'm using Smarty template. Is there something in smarty that needs to be disabled?
-
I'm creating a content management system. In the back end I have using Ckeditor (Formally FCKeditor). I have a large part of it done. When I hit "draft", I save the article into a draft table as a row with an incrementing ID number. mysql_query("INSERT INTO `drafts` (content,date,title) VALUES('$db_content','$date','$article_title')") or die(mysql_error()); After that I return all those variables back the CKeditor so I can continue editing the article. But If I hit "draft" again it will create a duplicate copy. So what I want to do is get the ID field and place it in a hidden field on the ckeditor and check for that during a post and then just update it again. Here is my code example: // fyi: Much other validation and stuff going on before here // Is this a draft save? if(isset($_POST['draft'])) { // Check for the hidden field draft id, if its set then this is not a new draft // but one that needs to be updated. if(isset($_POST['draft_id']) && is_numeric($_POST['draft_id'])) { $draft_id = $_POST['draft_id']; } // Check if the draft id is set, if it change our mysql query // to just update the corresponding draft rather then creating a duplicate copy. if(!empty($draft_id)) { mysql_query("UPDATE `drafts` SET content='{$db_content}', date='{$date}', title='{$article_title}' WHERE id={$draft_id}") or die(mysql_error()); $notice = "Your Draft has been updated"; } else { mysql_query("INSERT INTO `drafts` (content,date,title) VALUES('$db_content','$date','$article_title')") or die(mysql_error()); $notice = "Your Draft has been saved"; /* To do: This is first Draft save so grab the Article incrementing ID HERE... */ } ... So how I can get the ID on first draft save? I hope the logic of this makes sense. I know I could do another mysql query and maby just order it by ID then grab the first row. But that doesn't a good idea. What if there are 2 editors working saving drafts at the same time you know? You could get the wrong draft id.
-
Thats a strange IP. Its on the 4.0.0.0 range. Level 3 owns that range and I haven't heard of any of webhosts having that range.
-
[SOLVED] Calculate (add) all integers from mysql rows
keldorn replied to keldorn's topic in PHP Coding Help
It works now! I actually thought of that earlier but didn't do it, I should of so tried that. . $count now returns 99 hits. Thanks a bunch. -
[SOLVED] Calculate (add) all integers from mysql rows
keldorn replied to keldorn's topic in PHP Coding Help
To debug of course. The while() loop was not executed as thought. PHP ignored it for some reason. Here is the full code. <?php require 'libs/Smarty.class.php'; require 'config.php'; $smarty = new Smarty; //$smarty->caching = 2; //$smarty->cache_lifetime = 30; /* Remove caching for now if(!$smarty->is_cached('index.tpl')) { */ $results = array(); $link = mysql_connect($CONFIG['mysql_host'],$CONFIG['mysql_user'], $CONFIG['mysql_pass']) or die('Could not connect: ' . mysql_error()); $db_selected = mysql_select_db($CONFIG['mysql_db'],$link) or die('Could not contact the database'); $result = mysql_query('SELECT * FROM `links` ORDER BY id DESC LIMIT 0, 500')or die(mysql_error()); while ($line = mysql_fetch_assoc($result)) { $total_links[] = $line; //$count += $line['hits']; This here would return 35759 hits } //Get Todays Date in year-month-day $date = date("Y-m-d"); // Only grab the rows where the date = today's $date. $Hits = mysql_query('SELECT * FROM links WHERE date=' . $date . ' ORDER BY id DESC LIMIT 0,500') or die('Could not connect: ' . mysql_error()); // PHP ignores this for some reason. while ($line = mysql_fetch_assoc($Hits)) { $count += $line['hits']; } /* bracket from if caching } */ $total_rows = mysql_num_rows($result); //Assign the results to smarty $smarty->assign('result', $total_links); $smarty->assign('result', $total_hits); $smarty->assign('title',$title); $smarty->assign('total', $total_rows); $smarty->compile_check = true; $smarty->debugging = false; $smarty->display('index.tpl'); ?>