posts.php (5841B)
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 1.0dev 1/12/2014 17:16 9 */ 10 ############################### 11 # include files from root dir # 12 ############################### 13 $root_1 = realpath($_SERVER["DOCUMENT_ROOT"]); 14 $currentdir = getcwd(); 15 $root_2 = str_replace($root_1, '', $currentdir); 16 $root_3 = explode("/", $root_2); 17 if ($root_3[1] == 'core') { 18 $root = realpath($_SERVER["DOCUMENT_ROOT"]); 19 }else{ 20 $root = $root_1 . '/' . $root_3[1]; 21 } 22 include($root . '/core/config/connect.db.inc.php'); 23 //Check if Database connection established 24 if (mysqli_connect_errno()) { 25 printf("Verbindung fehlgeschlagen: %s\n", mysqli_connect_error()); 26 exit(); 27 } 28 if ($resultat = $connection->query('SELECT id FROM posts')) { 29 while($daten = $resultat->fetch_object() ){ 30 $post_id_clean = $daten->id; 31 #var_dump($daten); 32 } 33 #$resultat->close(); 34 } 35 if ($resultat = $connection->query("SELECT id FROM posts WHERE category LIKE '$category'")) { 36 while($daten = $resultat->fetch_object() ){ 37 $category_id_clean = $daten->id; 38 #var_dump($daten); 39 } 40 #$resultat->close(); 41 } 42 if ($resultat = $connection->query("SELECT id FROM posts WHERE author LIKE '$author'")) { 43 while($daten = $resultat->fetch_object() ){ 44 $author_id_clean = $daten->id; 45 #var_dump($daten); 46 } 47 } 48 next_id_only(); 49 next_id_category(); 50 next_id_author(); 51 function next_id_category(){ 52 read_category(); 53 } 54 function next_id_author(){ 55 read_author(); 56 } 57 function next_id_only(){ 58 read_only(); 59 } 60 function read_only(){ 61 global $id, $connection, $read, $post_id_clean; 62 global $post_id, $post_title, $post_text, $post_author, $post_date, $post_category, $post_text_short; 63 if ($resultat = $connection->query("SELECT id,title,text,author,category,date FROM posts WHERE id LIKE '$id'")) { 64 //echo 'SELECT * FROM posts WHERE id LIKE '.$id; 65 //Put database data in variables 66 while($daten = $resultat->fetch_object() ){ 67 //var_dump ($daten); 68 $post_id = $daten->id; 69 $post_title = $daten->title; 70 $post_text = $daten->text; 71 $post_author = $daten->author; 72 $post_date = $daten->date; 73 $post_category = $daten->category; 74 $post_text_short = shortText($post_text,300); 75 } 76 $resultat->close(); 77 } else { 78 global $id, $connection, $read, $post_id_clean; 79 global $post_id, $post_title, $post_text, $post_author, $post_date, $post_category; 80 global $error; 81 $error = "1"; 82 echo "Nothing to read from Database!"; 83 $post_id = "Database error!"; 84 $post_title = "Database error!"; 85 $post_text = "Database error!"; 86 $post_author = "Database error!"; 87 $post_date = "Database error!"; 88 $post_category = "Database error!"; 89 $post_text_short = "Database error!"; 90 } 91 } 92 93 94 function read_category(){ 95 global $id, $category, $connection, $read, $category_id_clean; 96 global $post_id, $post_title, $post_text, $post_author, $post_date, $post_category, $post_text_short; 97 if ($resultat = $connection->query("SELECT id,title,text,author,category,date FROM posts WHERE category LIKE '$category' AND id LIKE '$id'")) { 98 //echo 'SELECT * FROM posts WHERE id LIKE '.$id; 99 //Put database data in variables 100 while($daten = $resultat->fetch_object() ){ 101 //var_dump ($daten); 102 $post_id = $daten->id; 103 $post_title = $daten->title; 104 $post_text = $daten->text; 105 $post_author = $daten->author; 106 $post_date = $daten->date; 107 $post_category = $daten->category; 108 $post_text_short = shortText($post_text,300); 109 } 110 $resultat->close(); 111 } else { 112 global $id, $category, $connection, $read, $category_id_clean; 113 global $post_id, $post_title, $post_text, $post_author, $post_date, $post_category; 114 global $error; 115 $error = "1"; 116 echo "Nothing to read from Database!"; 117 $post_id = "Database error!"; 118 $post_title = "Database error!"; 119 $post_text = "Database error!"; 120 $post_author = "Database error!"; 121 $post_date = "Database error!"; 122 $post_category = "Database error!"; 123 $post_text_short = "Database error!"; 124 } 125 } 126 127 function read_author(){ 128 global $id, $author, $connection, $read, $author_id_clean; 129 global $post_id, $post_title, $post_text, $post_author, $post_date, $post_category, $post_text_short; 130 if ($resultat = $connection->query("SELECT id,title,text,author,category,date FROM posts WHERE author LIKE '$author' AND id LIKE '$id'")) { 131 //echo 'SELECT * FROM posts WHERE id LIKE '.$id; 132 //Put database data in variables 133 while($daten = $resultat->fetch_object() ){ 134 //var_dump ($daten); 135 $post_id = $daten->id; 136 $post_title = $daten->title; 137 $post_text = $daten->text; 138 $post_author = $daten->author; 139 $post_date = $daten->date; 140 $post_category = $daten->category; 141 $post_text_short = shortText($post_text,300); 142 } 143 $resultat->close(); 144 } else { 145 global $id, $author, $connection, $read, $author_id_clean; 146 global $post_id, $post_title, $post_text, $post_author, $post_date, $post_category; 147 global $error; 148 $error = "1"; 149 echo "Nothing to read from Database!"; 150 $post_id = "Database error!"; 151 $post_title = "Database error!"; 152 $post_text = "Database error!"; 153 $post_author = "Database error!"; 154 $post_date = "Database error!"; 155 $post_category = "Database error!"; 156 $post_text_short = "Database error!"; 157 } 158 } 159 160 161 162 function shortText($string,$lenght) { 163 if(strlen($string) > $lenght) { 164 $string = substr($string,0,$lenght)."[...]"; 165 $string_ende = strrchr($string, " "); 166 $string = str_replace($string_ende," [...]", $string); 167 } 168 return $string; 169 } 170 //$longtext = $post_text; 171 172 173 174 175 176 177 178 179 // Close database connection 180 $connection->close();