posts_new.php (6198B)
1 <?php 2 /** 3 * Get posts from Database 4 * 5 * This script will the post variables with the database data. 6 * 7 * @author Marcel Radzio <info@nordgedanken.de> 8 * @version 2.0dev 1/12/2014 17:16 9 */ 10 class posts{ 11 ############################### 12 # include files from root dir # 13 ############################### 14 $root_1 = realpath($_SERVER["DOCUMENT_ROOT"]); 15 $currentdir = getcwd(); 16 $root_2 = str_replace($root_1, '', $currentdir); 17 $root_3 = explode("/", $root_2); 18 if ($root_3[1] == 'core') { 19 $root = realpath($_SERVER["DOCUMENT_ROOT"]); 20 }else{ 21 $root = $root_1 . '/' . $root_3[1]; 22 } 23 24 ######################## 25 # check if mysqli work # 26 ######################## 27 if (mysqli_connect_errno()) { 28 printf("Connectin failed: %s\n", mysqli_connect_error()); 29 exit(); 30 } 31 32 ################## 33 # text shortener # 34 ################## 35 function shortText($string,$lenght) { 36 if(strlen($string) > $lenght) { 37 $string = substr($string,0,$lenght)."[...]"; 38 $string_ende = strrchr($string, " "); 39 $string = str_replace($string_ende," [...]", $string); 40 } 41 return $string; 42 } 43 44 ##################################### 45 # get needed vars for getting posts # 46 ##################################### 47 function preparePostAll(){ 48 if ($resultat = $connection->query('SELECT id FROM posts')) { 49 while($daten = $resultat->fetch_object() ){ 50 $post_id_clean = $daten->id; 51 } 52 } 53 } 54 function preparePostCategory(){ 55 if ($resultat = $connection->query("SELECT id FROM posts WHERE category LIKE '$category'")) { 56 while($daten = $resultat->fetch_object() ){ 57 $category_id_clean = $daten->id; 58 } 59 } 60 } 61 function preparePostAuthor(){ 62 if ($resultat = $connection->query("SELECT id FROM posts WHERE author LIKE '$author'")) { 63 while($daten = $resultat->fetch_object() ){ 64 $author_id_clean = $daten->id; 65 } 66 } 67 } 68 69 ################### 70 # get posts by id # 71 ################### 72 function read_only(){ 73 global $id, $connection, $read, $post_id_clean; 74 global $post_id, $post_title, $post_text, $post_author, $post_date, $post_category, $post_text_short; 75 if ($resultat = $connection->query("SELECT id,title,text,author,category,date FROM posts WHERE id LIKE '$id'")) { 76 //Put database data in variables 77 while($daten = $resultat->fetch_object() ){ 78 //var_dump ($daten); 79 $post_id = $daten->id; 80 $post_title = $daten->title; 81 $post_text = $daten->text; 82 $post_author = $daten->author; 83 $post_date = $daten->date; 84 $post_category = $daten->category; 85 $post_text_short = shortText($post_text,300); 86 } 87 $resultat->close(); 88 }else{ 89 global $id, $connection, $read, $post_id_clean; 90 global $post_id, $post_title, $post_text, $post_author, $post_date, $post_category; 91 global $error; 92 $error = "1"; 93 echo "Nothing to read from Database!"; 94 $post_id = "Database error!"; 95 $post_title = "Database error!"; 96 $post_text = "Database error!"; 97 $post_author = "Database error!"; 98 $post_date = "Database error!"; 99 $post_category = "Database error!"; 100 $post_text_short = "Database error!"; 101 } 102 } 103 104 ######################## 105 # get post by category # 106 ######################## 107 function read_category(){ 108 global $id, $category, $connection, $read, $category_id_clean; 109 global $post_id, $post_title, $post_text, $post_author, $post_date, $post_category, $post_text_short; 110 if ($resultat = $connection->query("SELECT id,title,text,author,category,date FROM posts WHERE category LIKE '$category' AND id LIKE '$id'")) { 111 //echo 'SELECT * FROM posts WHERE id LIKE '.$id; 112 //Put database data in variables 113 while($daten = $resultat->fetch_object() ){ 114 //var_dump ($daten); 115 $post_id = $daten->id; 116 $post_title = $daten->title; 117 $post_text = $daten->text; 118 $post_author = $daten->author; 119 $post_date = $daten->date; 120 $post_category = $daten->category; 121 $post_text_short = shortText($post_text,300); 122 } 123 $resultat->close(); 124 }else{ 125 global $id, $category, $connection, $read, $category_id_clean; 126 global $post_id, $post_title, $post_text, $post_author, $post_date, $post_category; 127 global $error; 128 $error = "1"; 129 echo "Nothing to read from Database!"; 130 $post_id = "Database error!"; 131 $post_title = "Database error!"; 132 $post_text = "Database error!"; 133 $post_author = "Database error!"; 134 $post_date = "Database error!"; 135 $post_category = "Database error!"; 136 $post_text_short = "Database error!"; 137 } 138 } 139 140 ###################### 141 # get post by author # 142 ###################### 143 function read_author(){ 144 global $id, $author, $connection, $read, $author_id_clean; 145 global $post_id, $post_title, $post_text, $post_author, $post_date, $post_category, $post_text_short; 146 if ($resultat = $connection->query("SELECT id,title,text,author,category,date FROM posts WHERE author LIKE '$author' AND id LIKE '$id'")) { 147 //echo 'SELECT * FROM posts WHERE id LIKE '.$id; 148 //Put database data in variables 149 while($daten = $resultat->fetch_object() ){ 150 //var_dump ($daten); 151 $post_id = $daten->id; 152 $post_title = $daten->title; 153 $post_text = $daten->text; 154 $post_author = $daten->author; 155 $post_date = $daten->date; 156 $post_category = $daten->category; 157 $post_text_short = shortText($post_text,300); 158 } 159 $resultat->close(); 160 } else { 161 global $id, $author, $connection, $read, $author_id_clean; 162 global $post_id, $post_title, $post_text, $post_author, $post_date, $post_category; 163 global $error; 164 $error = "1"; 165 echo "Nothing to read from Database!"; 166 $post_id = "Database error!"; 167 $post_title = "Database error!"; 168 $post_text = "Database error!"; 169 $post_author = "Database error!"; 170 $post_date = "Database error!"; 171 $post_category = "Database error!"; 172 $post_text_short = "Database error!"; 173 } 174 } 175 } 176 177 ########################### 178 # manuall loop workaround # 179 ########################### 180 function next_id_category(){ 181 preparePostCategory(); 182 read_category(); 183 } 184 function next_id_author(){ 185 preparePostAuthor(); 186 read_author(); 187 } 188 function next_id_only(){ 189 preparePostAll(); 190 read_only(); 191 } 192 193 next_id_only(); 194 next_id_category(); 195 next_id_author(); 196 197 ?>