PDA

View Full Version : مبتدی: نحوه دریافت داده ها از دیتابیس به صورت آرایه



amirmohammad76
پنج شنبه 03 تیر 1395, 01:44 صبح
با سلام خدمت تمامی دوستان ، من یک موتور قالب درست کردم و میخوام که اخبار سایتم رو با اون نمایش بدم ولی تو کد نویسی کمی مشکل دارم .
این فایل template.class.php هستش :

<?php class Template { protected $file; protected $values = array(); public function __construct($file) { $this->file = $file; } public function set($key, $value) { $this->values[$key] = $value; } public function output() { if (!file_exists($this->file)) { return "Error loading template file ($this->file).<br />"; } $output = file_get_contents($this->file); foreach ($this->values as $key => $value) { $tagToReplace = "[@$key]"; $output = str_replace($tagToReplace, $value, $output); }
return $output; } static public function merge($templates, $separator = "\n") { $output = ""; foreach ($templates as $template) { $content = (get_class($template) !== "Template") ? "Error, incorrect type - expected Template." : $template->output(); $output .= $content . $separator; } return $output; } }
?>
این هم post.tpl :

<article class="post"> <div class="wrapper"><header><span>[@title]</span><ul class="post-info"><li class="date">[@pdate]</li><li class="category">[@category]</li></ul></header><div class="pbod">[@content]</div><footer><a href="more.php?id=[@id]" title="[@title]" class="more">ادامه مطلب</a></footer></div></article>
و در آخر فایل index.php :

<?php
include("template.class.php"); /** * Defines an array for the posts. * Each value of the array is an array itself with each user's data. * Typically this would be loaded from a database. */ include('../config.php'); $postQuery = mysql_query("SELECT * FROM posts ORDER BY id"); $postArrays = mysql_fetch_array($postQuery,MYSQL_ASSOC); $postArray = array( array("id" => "1", "title" => "Title", "content" => "Content", "pdate" => "2016", "category" => "category") ); /** * Loop through the posts and creates a template for each one. * Because each user is an array with key/value pairs defined, * we made our template so each key matches a tag in the template, * allowing us to directly replace the values in the array. * We save each template in the $postsTemplates array. */ foreach($postArray as $post) {// posts = postarray , user = post $row = new Template("post.tpl"); foreach ($post as $key => $value) { $row->set($key, $value); } $postsTemplates[] = $row; //postsTemplate = postsTemplatE } /** * Merges all our posts' templates into a single variable. * This will allow us to use it in the main template. */ $postsContents = Template::merge($postsTemplates); /** * Defines the main template and sets the posts' content. */ $postsList = new Template("list_posts.tpl"); $postsList->set("posts", $postsContents); echo $postsList->output(); ?>
مشکل من اینه که داده ها برای نمایش داده شدن نیاز دارن که از یک آرایه خوانده بشن که حالت صحیح آرایه به صورت زیر هستش :

$users = array( array("username" => "monk3y", "location" => "Portugal") , array("username" => "Sailor", "location" => "Moon") , array("username" => "Treix!", "location" => "Caribbean Islands") );
ولی وقتی من دستور mysql_fetch_array رو اجرا میکنم نه تنها فقط یک داده رو نشون میده بلکه شکل آرایه هم اینجوری نمیشه ، یعنی خروجی به صورت زیر درمیاد :

Array( [id] => 4 [title] => title [content] => index [morecontent] => more [category] => 1,2,3 [author] => امیر محمد [pdate] => 2016-06-21 [tags] => title,index,more,new,first,news,cms)
که این خروجی هم یک array() کم داره هم اینکه بجای "id" این رو پرینت میکنه [id] که اینجوری سیستم نمیشناسه ، حالا من نمیدونم که باید فایل class رو ویرایش کنم یا در دریافت داده هام تغییری ایجاد کنم.
ضمنا این رو هم باید بگم که من php رو دارم تازه و توسط خودم یاد میگیرم.

amirmohammad76
پنج شنبه 03 تیر 1395, 02:02 صبح
دوستان حل شد!