0

因此,如果满足条件,应用程序应该从数据库中读取并以这种格式打印在页面上: 在此处输入图像描述 这里是代码,我将在最后的 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 基本结构: 在此处输入图像描述

和我的输出(错误的): 在此处输入图像描述

现在,问题:我不知道为什么第一个数组 ($studenty1) 的第一个成员作为属性索引,imeiprezime 和 godina 有 indeks,3,3 - 它应该是 12345,dusan skoric, 1。第二个问题,为什么是第一个数组的最后一个成员的最后一个索引属性,用作接下来两个数组的每个成员的 ideks 属性(Studenti2,studenti3)

4

1 回答 1

0

好的,我已经解决了我的第一个问题: line studenti["indeks"] = $indeks in lib.php 出现问题,但仍然找不到第二个问题的答案

于 2012-05-26T19:22:04.133 回答