php mysql在条件下从数据库中读取

时间:2012-05-26 19:04:17

标签: php mysql

因此,如果符合条件,应用程序应从数据库中读取并以此格式在页面上打印: enter image description here 这里是代码,我将最终发布我的输出图像 lib.php:

    <?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

现在,问题: 我无法弄清楚为什么frist数组的第一个成员($ studenty1)作为属性索引,imeiprezime和godina有凹凸不平,3,3 - 它应该是12345,dusan skoric,1。 第二个问题,为什么最后一个indeks属性来自第一个数组的最后一个成员用作下一个数组的每个成员的ideks属性(Studenti2,studenti3)

1 个答案:

答案 0 :(得分:0)

好的,我已经解决了我的第一个问题: line studenti [“indeks”] = lib.php中的$ indeks出现问题,但仍无法找到第二个问题的答案