2012-05-26 32 views
0

所以,如果條件滿足的應用應該從數據庫中讀取並在此格式頁面上打印: enter image description here 這裏是代碼,我將張貼我的輸出的圖像到底 lib.php :PHP的MySQL的條件下,從數據庫中讀取

<?php 
$db_name = "ispit_septembar"; 
$db_user = "root"; 
$db_host = "localhost"; 
$db_pass = ""; 

function vratiStudente($godina) 
{ 
global $db_name,$db_user,$db_host,$db_pass; 
$studenti = array(); 
$link = mysql_connect("$db_host","$db_user","$db_pass") or die ("Nije moguca konekcija"); 

mysql_select_db("$db_name",$link) or die ("Nepostojeca baza"); 
$sql = mysql_query("SELECT * from studenti where godina = '$godina'"); 
while ($row = mysql_fetch_array($sql)) 
    { 
    $indeks = $row["indeks"]; 
    $imeiprezime = $row["imeiprezime"]; 
    $godina = $row["godina"]; 
    $studenti["indeks"] = $indeks; 
    $studenti[$indeks]["imeiprezime"] = $imeiprezime; 
    $studenti[$indeks]["godina"] = $godina; 

    } 
return $studenti; 
} 
function daLiPostojiStudent($indeks) 
{ 
    global $db_name,$db_user,$db_host,$db_pass; 
    $link = mysql_connect("$db_host","$db_user","$db_pass") or die ("Nije moguca konekcija"); 
    mysql_select_db("$db_name","$link") or die ("Nepostojeca baza"); 
    $sql = mysql_query("SELECT * from studenti where indeks = '$indeks'"); 
    $check = mysql_num_rows($sql); 
    if ($check >0) 
    { 
    $postoji = "postoji"; 
    } 
    else 
    { 
    $postoji = "ne postoji"; 
    } 
    return $postoji; 
} 
function dodajStudenta($indeks,$ime,$godina) 
{ 
    global $db_name,$db_user,$db_host,$db_pass; 
    $link = mysql_connect("$db_host","$db_user","$db_pass") or die ("Nije moguca konekcija"); 
    mysql_select_db("$db_name","$link") or die ("Nepostojeca baza"); 
    $result = daLiPostojiStudent($indeks); 
    if ($result == "postoji") 
    { 
    $postoji = "Student sa tim brojem indeksa postoji"; 
    return $postoji; 
    } 
    else 
    { 
    $sql = ("INSERT INTO studenti (indeks,imeprezime,godina) 
      VALUES ('$indeks','$ime','$godina' "); 
    mysql_query($sql); 
    return 1; 
    } 
} 
?> 

strana1.php:`

<?php 
include "lib.php"; 
include "Smarty/libs/Smarty.class.php"; 
$dodaj1 = "strana2.php?godina=1"; 
$dodaj2 = "strana2.php?godina=2"; 
$dodaj3 = "strana2.php?godina=3"; 


$studenti1 = vratiStudente(1); 
echo ($studenti1["indeks"]); 
$studenti2 = vratiStudente(2); 
$studenti3 = vratiStudente(3); 



$smarty= new Smarty(); 
$smarty->assign("dodaj1",$dodaj1); 
$smarty->assign("dodaj2",$dodaj2); 
$smarty->assign("dodaj3",$dodaj3); 
$smarty->assign("studenti1",$studenti1); 
$smarty->assign("studenti2",$studenti2); 
$smarty->assign("studenti3",$studenti3); 
$smarty->display("strana1.tpl"); 


?> 

strana1.tpl

<html> 
    <head> 
    </head> 
    <body> 
    Godina: 1 <a href ={$dodaj1}>Dodaj</a> 
<hr> 
<table> 

<tr> 
    <th>Indeks</th> 
    <th>Ime i Prezime </th> 
</tr> 
{foreach name="studenti1loop" item=student key=indeks from=$studenti1} 
<tr> 
    <td> {$indeks} </td> 
    <td> {$student.imeiprezime}</td> 
    <td>{$student.godina}</td> 
</tr> 
{/foreach} 
</table> 
{* druga godina*} 
Godina: 2 <a href ={$dodaj2}>Dodaj</a> 
<hr> 
<table> 

<tr> 
    <th>Indeks</th> 
    <th>Ime i Prezime </th> 
</tr> 
{foreach name="studenti2loop" item=student from =$studenti2} 
<tr> 
    <td> {$indeks} </td> 
    <td> {$student.imeiprezime}</td> 
    <td>{$student.godina}</td> 
</tr> 
{/foreach} 
</table> 
{* treca godina*} 
Godina: 3 <a href ={$dodaj3}>Dodaj</a> 
<hr> 
<table> 

<tr> 
    <th>Indeks</th> 
    <th>Ime i Prezime </th> 
</tr> 
{foreach name="studenti2loop" item=student from=$studenti3} 
<tr> 
    <td> {$indeks} </td> 
    <td> {$student.imeiprezime}</td> 
    <td>{$student.godina}</td> 
</tr> 
{/foreach} 
</table> 

</body> 

這是my_sql基本結構: enter image description here

和我的輸出(錯誤的): enter image description here

而現在,問題: 我想不通爲什麼弗里斯特數組的第一個成員( $ studenty1)作爲屬性索引,imeiprezime和godina有indegs,3,3 - 它應該是12345,dusan skoric,1. 第二個問題,爲什麼最後一個indeks屬性來自第一個數組的最後一個成員用作每個成員的ideks屬性下兩個數組(Studenti2,studenti3)

+0

人!這麼小的帖子... – Rahul

+0

哇!只是傾銷這麼多的信息。 cmon man,使我們的工作解碼wrk更容易通過只顯示要領.. – arijeet

回答

0

好了,我已經解決了我的第一個問題: 線studenti [「indeks」在lib.php = $ indeks是決策問題,但仍無法找到答案,第二個問題

+0

我已經解決了第二個問題,雖然我不知道這是否是最佳解決方案: in function vratiStudenta() –

+0

我已經解決了第二個問題也 –

+0

和第二個問題: 我錯過了關鍵=第二和第三循環在「strana1.tpl」 所有圖片抱歉,我試圖讓你easyer代碼應該做 –