|
查看: 892|回复: 11
|
紧急 php的问题
[复制链接]
|
|
|
php 里有没像 rand() 的乱数 但是 不会重复的command?
由于明天就需要用到 很紧急
我的program 是一个 exam 的program 。 但是 里面 是照我的database 里面的资料 1 开始照排
我要做 随机 我想不到有什么command 来做。 用rand() 会 重复
求求大大帮个忙 |
|
|
|
|
|
|
|
|
|
|
发表于 4-11-2008 07:40 PM
|
显示全部楼层
|
我想, 先隨機生成一個 id 再查找這個 id 是否有沒有被用. |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 4-11-2008 07:55 PM
|
显示全部楼层
index.php
<?php
$link = mysql_connect("localhost","root","");
$sql="SELECT * FROM exam";
$result = mysql_db_query("plt",$sql);
$total_rows=mysql_num_rows($result);
echo "<table border='1' align='center'>";
?>
<form method="post" action="answer.php">
<?php
for($a=1;$a<=$total_rows;$a++)
{
$field_data =mysql_fetch_object($result);
?>
<tr>
<td>Q<?php echo $a; ?></td>
<td>
<p><?php echo $field_data ->subject ?></p>
<p>
<input type="radio" name="<?php echo "$a"; ?>" value="1"><?php echo $field_data ->choose_1 ?>
<input type="radio" name="<?php echo "$a"; ?>" value="2"> <?php echo $field_data ->choose_2 ?>
<input type="radio" name="<?php echo "$a"; ?>" value="3"> <?php echo $field_data ->choose_3 ?>
<input type="radio" name="<?php echo "$a"; ?>" value="4"><?php echo $field_data ->choose_4 ?>
</p>
</td>
</tr>
<?php
}
mysql_close;
?>
<tr>
<td colspan="2"><p align="center"><input type="submit" name="submit" value="submit"></p></td>
</tr>
</form>
</table> |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 4-11-2008 07:55 PM
|
显示全部楼层
answer.php
<?php
$link = mysql_connect("localhost","root","");
$sql="SELECT * FROM exam";
$result = mysql_db_query("plt",$sql);
$total_rows=mysql_num_rows($result);
$b=0;
echo "<table border='1' align='center'>";
for($a=1;$a<=$total_rows;$a++)
{
$arr[]=$_REQUEST["$a"];
$field_data =mysql_fetch_object($result);
if($arr[$b]!=""){
?>
<tr>
<td>
<?php
echo "your answer is";
if($arr[$b]==$field_data ->answer)
{
echo " true";
}
else
{
echo " false";
}
if($arr[$b]!=$field_data ->answer){
?>
</td>
<td>the corrent answer is
<?php
echo $field_data ->answer;
?>
</td>
</tr>
<?php
}
$b++;
}
}
mysql_close;
?>
</table> |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 4-11-2008 07:56 PM
|
显示全部楼层
mysql database query
-- phpMyAdmin SQL Dump
-- version 2.11.6
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 03, 2008 at 10:10 PM
-- Server version: 5.0.51
-- PHP Version: 5.2.6
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `plt`
--
CREATE DATABASE `plt` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `plt`;
-- --------------------------------------------------------
--
-- Table structure for table `exam`
--
CREATE TABLE `exam` (
`code` int(4) unsigned zerofill NOT NULL auto_increment,
`subject` text,
`choose_1` text,
`choose_2` text,
`choose_3` text,
`choose_4` text,
`answer` varchar(1) default NULL,
PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
--
-- Dumping data for table `exam`
--
INSERT INTO `exam` (`code`, `subject`, `choose_1`, `choose_2`, `choose_3`, `choose_4`, `answer`) VALUES
(0001, 'What does PHP stand for?', 'Personal Home Page', 'PHP: Hypertext Preprocessor', 'Private Home Page', 'Personal Hypertext Processor', '2'),
(0002, '1+1 = ?', '1', '2', '3', '4', '2'),
(0003, 'How do you write "Hello World" in PHP', '"Hello World";', 'Document.Write("Hello World");', 'Write("Hello World");', 'echo "Hello World";', '4'),
(0004, 'All variables in PHP start with which symbol?', '!', '&', '$', '*', '3'),
(0005, 'What is the correct way to end a PHP statement? ', '/php', '.', ';', 'New line', '3'),
(0006, 'How do you get information from a form that is submitted using the "get" method?', 'Request.Form;', '$_GET[];', 'Request.QueryString; ', '$get}{', '2'),
(0007, 'Include files must have the file extension ".inc"', 'false', 'true', 'other', '.include', '2'),
(0008, 'What is the correct way to include the file "time.inc" ?', 'require("time.inc");', 'include file="time.inc"', 'include_file("time.inc");', 'include file="time.inc"', '1'),
(0009, 'What is the correct way to create a function in PHP?', 'create myFunction()', 'function myFunction()', 'new_function myFunction()', 'new_myFunction()', '2'),
(0010, 'What is the correct way to open the file "time.txt" as readable?', 'fopen("time.txt","r+");', 'open("time.txt");', 'fopen("time.txt","r");', 'open("time.txt","read");', '3'); |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 4-11-2008 07:56 PM
|
显示全部楼层
不太明白你的意思 |
|
|
|
|
|
|
|
|
|
|
发表于 5-11-2008 08:52 AM
|
显示全部楼层
shuffle($array);
例子:
<?
$a = array(1,2,3,4,5,6,7);
shuffle($a);
print_r($a);
?> |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 5-11-2008 10:10 AM
|
显示全部楼层
$a = array(1,2,3,4,5,6,7);
shuffle($a);
print_r($a);
没看过 能解释下吗 |
|
|
|
|
|
|
|
|
|
|
发表于 5-11-2008 12:47 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 6-11-2008 09:34 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 6-11-2008 02:01 PM
|
显示全部楼层
只要写出来的能走,又符合你的要求。。就没有所谓的对与错
只有效率高/效率低罢了
p.s/ index.php里面的$b=$a是要做什么的? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 6-11-2008 07:04 PM
|
显示全部楼层
$b=$a;
没用的 我忘了delete |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|