Page 1 of 1

Database variable table spliting

Posted: Sun Jul 15, 2007 11:04 am
by andrewcrawford
Hi all,

Code: Select all

<?php

mysql_connect("database", "username", "password") or die(mysql_error()); 
mysql_select_db("databasename") or die(mysql_error()); 

$query="select * from table";

mysql_query($query);
$result=mysql_query($query);
$num=mysql_num_rows($result);

mysql_close();

$i=0;

echo"<center><table border=1><tr>";
echo "<td>Variable1</td>";
echo "<td>Variable2</td>";
echo "<td>Variable3</td>";
echo "<td>Variable4</td>";

while ($i < $num)

{

$variable1=mysql_result($result,$i,"variable1");
$variable2=mysql_result($result,$i,"variable2");
$variable3=mysql_result($result,$i,"variable3");
$variable4=mysql_result($result,$i,"variable4");

echo "<tr><td>$variable1</td><br>";
echo "<tr><td>$variable2</td><br>";
echo "<tr><td>$variable3</td><br>";
echo "<tr><td>$variable4</td><br>";

$i++;

}

echo "</table></center></font>";

?>
The above is code i have developed to display information from a database, for security reasons i have removed all information relating to the database and replaced it with dummy information.

I am wanting to to split variable information into serpate tables, ie if variable contained info1 na dinfo2 it would split info1 into a serpate table and info2 into a another table.

Eventally i would want it to split the variable into serpate pages with links ot them and i would want variable2 to be split the same way after variable1

What sort of function will i require? i have a fair idea it will be very omplex code

Posted: Sun Jul 15, 2007 11:40 am
by feyd
Why would each variable be its own page?

Posted: Sun Jul 15, 2007 11:51 am
by andrewcrawford
when the variable could exceed 200 different items with many different subitems then having it on one page could be very long page to scroll down hence why th need to split the page in teh future, but if it jsut as easy then i rather do it now

ill try give you a example

variable1 variable2 variable3 variable4
info1 1 1 details
info1 1 2 details
info1 1 3 details
info1 1 4 details
info1 1 5 details
info1 1 6 details
info1 1 7 details
info1 1 8 details
info1 1 9 details
info1 1 10 details
info1 1 11 details
info1 1 12 details
info1 1 13 details
info1 1 14 details
info1 1 15 details
info1 1 16 details
info1 1 17 details
info1 1 18 details
info1 1 19 details
info1 1 20 details
info1 1 21 details
info1 1 22 details
info1 1 23 details
info1 2 1 details
info1 2 2 details
info1 2 3 details
info1 2 4 details
info1 2 5 details
info1 2 6 details
info1 2 7 details
info1 2 8 details
info1 2 9 details
info1 2 10 details
info1 2 11 details
info1 2 12 details
info1 2 13 details
info1 2 14 details
info1 2 15 details
info1 2 16 details
info1 2 17 details
info1 2 18 details
info1 2 19 details
info1 2 20 details
info1 2 21 details
info1 2 22 details
info1 2 23 details
info2 1 1 details
info2 1 2 details
info2 1 3 details
info2 1 4 details
info2 1 5 details
info2 1 6 details
info2 1 7 details
info2 1 8 details
info2 1 9 details
info2 1 10 details
info2 1 11 details
info2 1 12 details
info2 1 13 details
info2 1 14 details
info2 1 15 details
info2 1 16 details
info2 1 17 details
info2 1 18 details
info2 1 19 details
info2 1 20 details
info2 1 21 details
info2 1 22 details
info2 1 23 details
info2 1 24 details
info2 1 25 details
info2 1 26 details

and that would be only with say a info1 and info2 and then variable2 could still get higher

so vairable1 - info1 and variable2 1 would be it own page and variable1 - info1 and variable2 2 would be its own page etc

Posted: Sun Jul 15, 2007 11:54 am
by feyd
Sorry, you're not making a lot of sense, but it seems you're referring to what's called "pagination."

Posted: Sun Jul 15, 2007 12:21 pm
by andrewcrawford
Hiya, i just done a search on the meaning of that word i think it might be what i am trying to explain.

But since my english is poor due to dsylexica i will make a sample website via html so i can show what i am meaning then try explain what i want the php to do automatically so i owuldnt need to make html up for ever page and what the database is to do

Posted: Thu Jul 26, 2007 12:18 pm
by andrewcrawford
i have wrote up some php code to get the information out of the dtabase and display it but the problem is it is not the way i like. here is a sample of how it looks just now with the current php code current sample this is written in purely html as a example of what it is like.

Basically i want it to work the way i haev set out this pure html pages layout i want to display how i liek the informationf rom the database to be displayed, i have also worked out roughly the pseudocode/alogrithms of how it work but i do nto think there complete

OK

page 1

query database for variables
information in variable1 make into a link for that variable if it doesnt exist otherwise display page
if page does nto exist make page with default layout page

page 2

query database for variable1 = to link name
information in variable2 make into link/links for that variable if it doesnt exist otherwise display page
if the page for variable does nto exist make page with default layout page

page 3

query database for variable1 + variable 2 = to previous page information
information in variable3 and variable 4 display in default layout page
information in variable4 make into a link for that variable if it doesnt exist
if the variable4 page does nto exist make page with default layout

some additional information explain my pseudocode since my english is poor due to dsylexica

ok , the first page will read teh database and from field1 will determin how many unique entries there is, so for example you have '12' info1 '2' info2 and '45' info3, it will then displaya page with links to info1,info2,info3 if there anew entrie from the last time it was looked at it will then create a link to the page for the new entrie in field1 and also create the page for it using a prefine layout page

then on the page say info1, it will connect to the databse again, it will display the information from field 1 as a title so to say, then field5 will make up the middle of the page and field 2 will be then made into links again the same as above if ti exist it display the link if it does nto it will create the link, then it will create that page witht he default layout again.

on the page that is linked from field2 it will then connect to the database and grab the information again, it will then display field 1 the same as before and put field 2 below it as a heaidng, then it will display fields 3 and 4 in a table, and field 4 will link to another page again if ti exist ti will display if it does not it will create ti froma default layout page

Posted: Thu Jul 26, 2007 2:15 pm
by andrewcrawford
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


ive just fired up some quick code roughly how page1 should work but since i ma not excately sure what php function can do each of the things i want to do some of it iwll look weird

Code: Select all

<html>
<body>

<?php

database details

$query = "select distinct field1 from table order by field1";

mysql_query($query);
$result=mysql_query($query);
$num=mysql_num_rows($result);

mysql_close();

$i=0;

while ($i < $num)
  {
    $field1=mysql_result($result,$i,"field1");
}

$location = "/pages/";
$pages = array(scandir $location);
$pagenames = (remove .php from $pages);

if ($result == $pagenames)
  {
    echo "<a href=$location.$pages>$field1</a>";
}
else
  {
    create page from defaultpage then echo "<a href=$location.$pages>$field1</a>";
}

?>

</body>
</html>
$pages = array(search $location); changed to $pages = array(scandir $location); scandir seems to be a function to scan for files in a folder


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Jul 26, 2007 5:35 pm
by andrewcrawford
ok been fiddling aroudn with this and now created this code

first it searchs the database and select distinct field1 and orders ti by field1

then it arrays the results from field1

then i set a variable for the location of the pages

then i array the pages into a variable with scandir

then i remove teh file extension .php

then i check that the databse entires are the same as the current pages

if they are the same it echos links to the pages

if they are not it then sends the it to another page to create a page for it using a default layout (this i have not done yet

Code: Select all

<html> 
<body> 

<?php 

database details 

$query = "select distinct field1 from table order by field1"; 

mysql_query($query); 
$result=mysql_query($query); 
$num=mysql_num_rows($result); 

mysql_close(); 

$i=0; 

while ($i < $num) 
  { 
    $field1=mysql_array($result); 
} 

$location = "/pages/"; 
$pages = array(scandir $location); 
$pagenames = {$ext = strrchr($pages, '.');

if($ext !== false) 
{ 
$pages = substr($pages, 0, -strlen($ext)); 
} 
return $pages; 
}}

if ($field1 == $pagenames) 
  { 
    echo link($location.$pages)$field1; 
} 
else 
  { 
    header(location: "createnewpage.php?=$field1"; 
} 

?> 

</body> 
</html>
ok i need to know if this might work or if there will be tweaks needed

Posted: Thu Jul 26, 2007 5:59 pm
by superdezign
andrewcrawford wrote:ok i need to know if this might work or if there will be tweaks needed
Have you tried it? I refuse to read it until you tried it yourself.

Posted: Fri Jul 27, 2007 4:30 am
by andrewcrawford
Yes i had tried it myself just some of the functions names where not being recongised i think but i now got it giving errors

i have modifyed the code now

Code: Select all

<html> 
<body> 

<?php 

include 'functions.php';
//include 'database.php';
include 'databaselocal.php';

mysql_connect($host,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "select distinct field1 from table order by field1"; 

mysql_query($query); 
$result=mysql_query($query); 
$num=mysql_num_rows($result); 

mysql_close(); 

$i=0; 

while ($i < $num) 
  { 
    $field1=mysql_fetch_array($result); 
} 

$location = "pages"; 
$pages = array(scandir $location); 
$pagenames = function extstrip($pages);

if ($field1 == $pagenames) 
  { 
    echo link($location.$pages)$field1; 
} 
else 
  { 
    header(location: "createnewpage.php?=$field1"); 
} 

?> 

</body> 
</html>
the functions.php is as follows

Code: Select all

<?php

function extstrip($pages);

{$ext = strrchr($pages, '.'); 

if($ext !== false) 
{ 
$pages = substr($pages, 0, -strlen($ext)); 
} 
return $pages; 
}

?>
the error it gives is as follows

Parse error: parse error, unexpected T_VARIABLE, expecting ')' in c:\program files\easyphp1-7\www\test\test.php on line 29

this is line 29 $pages = array(scandir $location); so you know where to look but i suspect the fault sis on line28

thank you in advance for any help :)

Posted: Fri Jul 27, 2007 10:10 am
by superdezign
andrewcrawford wrote:this is line 29 $pages = array(scandir $location); so you know where to look but i suspect the fault sis on line28
Line 28 is fine. Line 29 is... strange. What are you trying to do? Restrict the type or create an associative index? Or is that a function?

EDIT: Also, neither of these make sense:

Code: Select all

$pagenames = function extstrip($pages);

Code: Select all

header(location: "createnewpage.php?=$field1");

Posted: Fri Jul 27, 2007 4:14 pm
by andrewcrawford
superdezign wrote:
andrewcrawford wrote:this is line 29 $pages = array(scandir $location); so you know where to look but i suspect the fault sis on line28
Line 28 is fine. Line 29 is... strange. What are you trying to do? Restrict the type or create an associative index? Or is that a function?

EDIT: Also, neither of these make sense:

Code: Select all

$pagenames = function extstrip($pages);

Code: Select all

header(location: "createnewpage.php?=$field1");
on line 29 i am trying to scan the directory of location for all files in it and array it so i can check the fields in teh database against it

Code: Select all

$pagenames = function extstrip($pages);
this is the function i am callign to strip the .php from the name

Code: Select all

header(location: "createnewpage.php?=$field1");
this will send the code to create page for field one if it doesnt exist

Posted: Fri Jul 27, 2007 5:28 pm
by superdezign
andrewcrawford wrote:on line 29 i am trying to scan the directory of location for all files in it and array it so i can check the fields in teh database against it
scandir() is a function that takes a directory as the first parameter and returns an array. The "syntax" you are using shows that you have a basic idea of what you want, but it doesn't show an understanding of the language. :?

The scandir function returns an array, so there is little to no reason for you to put that array into another array, as you are doing. Also, functions require parentheses to take arguments, which is why PHP is telling you that the parser hit an unexpected T_VARIABLE (variable token), which is $location. You're code is improperly formatted. You should simply set $pages equal to scandir($location).
andrewcrawford wrote:

Code: Select all

$pagenames = function extstrip($pages);
this is the function i am callign to strip the .php from the name
That is not the proper way to call a function. The function keyword specifies that you are defining a function, not using one.
andrewcrawford wrote:

Code: Select all

header(location: "createnewpage.php?=$field1");
this will send the code to create page for field one if it doesnt exist
That is not the way to format a header. The header function is not magic. It simply takes in exactly what you give as arguments and sends them as headers. Headers are plain-text, so the entire header would be a string. In your code, 'location: ' is not part of the string and will throw a nasty parser error when you actually get the parser to read that far into your code.


I'd suggest that you take a few more beginner's tutorials before you get stuck on silly mistakes such as these so that you may strengthen your knowledge of the language.