digital_storm
10-04-2004, 03:30 PM
Hello!
I'm a newbie of PHP but have been using JPS for a long time so I'm familiar with dynamic webbprograming and objects etc.
In JSP you could after every selected item/object in a DATABASE-TABLE add that object to a vector and then return that vector with all objects in it to where its called from like:
----------selecting from databas---------------
Vector e= new Vector();
while(rs.next())[
Myobject m=new Object(); //An object thathave 2 parameters "datumskapad" and "ingress"
m.datumskapad=rs.getString("datumskapad");
m.ingress=rs.getString("ingress");
e.addElement(m);
}
.
.
.
//before "ENDTAG"
return e;
----------------------------------------------
How do I do that in PHP? I'am a really newbie of PHP and have been searching for using vectors in PHP but havent found any. Maybee there are a smarter way to do this in PHP. I have been using the syntax below for retrieving info from the same TABLE as above. Nyhet is a class which is called from hello.php
-----------------------Nyhet.php---------------
<?
class Nyhet
{
var $idnr;
var $datumskapad;
var $datumslut;
var $datumevent;
var $dkategori;
var $ingress;
var $text;
var $createdby;
function Nyhet(){
}
function printarticle()
{
echo "<table border=1 cellspacing=0 cellpadding=0>
<tr><td>" . $this->headline . " by " . $this->author . "</td></tr>
<tr><td>" . $this->story . "</td></tr></table>";
}
function getnyheter(){
$username="";
$password="";
$host="localhost";
$database="dtk";
mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM notes";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$datumskapad = mysql_result($result,$i,"datumskapad");
$ingress = mysql_result($result,$i,"ingress");
echo "<b>$datumskapad $ingress</b><br>";
$i++;
}
}
}
?>
-------------------------------------------------------
----------------------hello.php------------------------
<?php include('nyhet.php');?>
<html>
<body>
<?php
$nyhet=new Nyhet();
echo $nyhet->getnyheter();
?>
</body>
</html>
--------------------------------------------------------
There is a huge different what I can see because in this PHP example I lock the look of the info because in this PHP-example I have to include the look of the page in my echos-statement. In JSP I could just have returned the vector and in a for-loop go through the vector and apply the look of the page from where the function was called, like:
<%Vector e=MyjavaBean.getCurrentvector();%>
<table>
<%for(int i=0;i<m.size();i++){;%>
<% Myobjet m=(Myobject) e.elementAt(i);%>
<tr>
<td><%=m.datumskapad%></td>
<td><%=m.ingress%></</td>
</tr>
<%};%>
</table>
Is it possible to do this in PHP too?
Thanks!
I'm a newbie of PHP but have been using JPS for a long time so I'm familiar with dynamic webbprograming and objects etc.
In JSP you could after every selected item/object in a DATABASE-TABLE add that object to a vector and then return that vector with all objects in it to where its called from like:
----------selecting from databas---------------
Vector e= new Vector();
while(rs.next())[
Myobject m=new Object(); //An object thathave 2 parameters "datumskapad" and "ingress"
m.datumskapad=rs.getString("datumskapad");
m.ingress=rs.getString("ingress");
e.addElement(m);
}
.
.
.
//before "ENDTAG"
return e;
----------------------------------------------
How do I do that in PHP? I'am a really newbie of PHP and have been searching for using vectors in PHP but havent found any. Maybee there are a smarter way to do this in PHP. I have been using the syntax below for retrieving info from the same TABLE as above. Nyhet is a class which is called from hello.php
-----------------------Nyhet.php---------------
<?
class Nyhet
{
var $idnr;
var $datumskapad;
var $datumslut;
var $datumevent;
var $dkategori;
var $ingress;
var $text;
var $createdby;
function Nyhet(){
}
function printarticle()
{
echo "<table border=1 cellspacing=0 cellpadding=0>
<tr><td>" . $this->headline . " by " . $this->author . "</td></tr>
<tr><td>" . $this->story . "</td></tr></table>";
}
function getnyheter(){
$username="";
$password="";
$host="localhost";
$database="dtk";
mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM notes";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$datumskapad = mysql_result($result,$i,"datumskapad");
$ingress = mysql_result($result,$i,"ingress");
echo "<b>$datumskapad $ingress</b><br>";
$i++;
}
}
}
?>
-------------------------------------------------------
----------------------hello.php------------------------
<?php include('nyhet.php');?>
<html>
<body>
<?php
$nyhet=new Nyhet();
echo $nyhet->getnyheter();
?>
</body>
</html>
--------------------------------------------------------
There is a huge different what I can see because in this PHP example I lock the look of the info because in this PHP-example I have to include the look of the page in my echos-statement. In JSP I could just have returned the vector and in a for-loop go through the vector and apply the look of the page from where the function was called, like:
<%Vector e=MyjavaBean.getCurrentvector();%>
<table>
<%for(int i=0;i<m.size();i++){;%>
<% Myobjet m=(Myobject) e.elementAt(i);%>
<tr>
<td><%=m.datumskapad%></td>
<td><%=m.ingress%></</td>
</tr>
<%};%>
</table>
Is it possible to do this in PHP too?
Thanks!