473,395 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Building a simple array - some help needed !

290 100+
Hi,

I am stepping though a table and want to display the output after
the stepping through has finished and I have calculated the pagination.

So I think that I need to put the results into and array and then
print out the array a bit later on.

I am a bit rusty on creating arrays

This is my php:

Expand|Select|Wrap|Line Numbers
  1. while($row = mysql_fetch_assoc($result)){
  2.   extract($row);
  3.   $Rctr =$row_ctr+1;
  4.   $scn = strtolower(trim($sc_name));
  5.   $link1 = "/internet_marketing/online_tuition/".$SEOcat."/".$tute_id.".html";
  6.   $link2 = "/internet_guru/$scn/".$user_id.".html";
  7.  
  8.   echo "<div class=\"listerdiv\">
  9.   <h2>$Rctr) <a href=\"$link1\">$tute_head</a></h2>
  10.   <h3>By  <a href='$link2'>$sc_name</a></h3>
  11.   </div>";
  12.  
  13.  $row_ctr = $row_ctr + 1;
  14.  $ad_ctr = $ad_ctr + 1;            
  15. }  // end while 
  16.  
I realise that the data in inside an array called $row, but that is going to be replaced at the next loop of the while. Also the $link1 & 2 will change with each loop.

Its going to loop 20 times and then after its finished, I will calculate some
stuff and display the results of that stuff, and then I want to display the
results from those twenty loops.

So thats what I need the extra array for.
Can anyone help me out with this one ?
Jan 24 '09 #1
9 1703
Dormilich
8,658 Expert Mod 8TB
If you want to go for some crazy stuff you can put your results into objects
Expand|Select|Wrap|Line Numbers
  1. // example using PDO
  2. $array = $PDOstmt->fetchAll(PDO::FETCH_CLASS, 'your_class');
  3. // or
  4. $array = $PDOstmt->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'your_class');
the class methods are responsible for preparing the output, you just need to define the methods.
Expand|Select|Wrap|Line Numbers
  1. class your_class
  2. {
  3.     // properties according to the fieldnames
  4.  
  5.     function __construct($ini = "default")
  6.     {
  7.         // executed before or after creation (dep. on setting)
  8.     }
  9.  
  10.     function __toString() // called by echo, print, ...
  11.     {
  12.         $str = "<a href='$this->link2'>$this->sc_name</a>"; // just an example
  13.         return $str;
  14.     }
  15. }
and finally printing all out.
Expand|Select|Wrap|Line Numbers
  1. foreach ($array as $obj)
  2. {
  3.     echo $obj; // calling the __toString() method
  4. }
this is a rather rough and short proposal, but it's not much code and lots of fun.
Jan 24 '09 #2
jeddiki
290 100+
Wow !! -
I don't do any php with objects and classes.

I haven't really got my head round it yet.

Actually - I dont even know what PDO is !
Is it short for something ?
Is fetchAll() a php cmd ?

I don't know if this is the time to start with objects and stuff
it looks like a completely different coding language !
Jan 24 '09 #3
Dormilich
8,658 Expert Mod 8TB
@jeddiki
time to get started

@jeddiki
PDO = PHP Data Objects, PHP: PDO - Manual
fetchAll() is one of PDO's methods to get the SQL results. (basicly, take all results and put them into an array)

@jeddiki
only at the beginning, the biggest problem is to understand how to use them.
introduction to objects: PHP: Classes and Objects (PHP 5) - Manual
Jan 24 '09 #4
jeddiki
290 100+
I suppose I had better search for a good beginners tute ?

Can you recommend any to get me started ?
Jan 24 '09 #5
Dormilich
8,658 Expert Mod 8TB
first, read a bit on PHP's website (see above).

right now I have no tutorial on my mind, but a very good source for any PHP programming is "PHP Power Programming" (PDF available for free, just google for it)

a good phrase for google searching is probably "OOP Tutorial"

regards

and of cause you can always ask us on bytes.com *g*
Jan 24 '09 #6
jeddiki
290 100+
Hi I have been trying out a bit of OOP !

This is what I wrote:



Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class person {
  3.   var $name;     
  4.       public $height;
  5.       protected $social_insurance;
  6.       private $pinn_number;
  7.  
  8.  
  9.   function __construct($persons_name) {
  10.     $this->name = $persons_name;
  11.   }
  12.  
  13.   function set_name($new_name) {  
  14.    $this->name = $new_name;            
  15.   }
  16.  
  17.     function set_pinn_number($new_pinn_number) {  
  18.    $this->pinn_number = $new_pinn_number;            
  19.   }
  20.  
  21.   function get_name() {
  22.     return $this->name;
  23.   }
  24.  
  25.     function get_pinn_number() {
  26.      return $this->pinn_number;
  27.   }     
  28. } // end class
  29.  
  30.  
  31. ?>
  32.  
and
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>OOP in PHP</title>
  6.  
  7. <?php include("class_lib.php"); ?>
  8.  
  9. </head>
  10. <body>
  11.  
  12. <?php
  13.       $jimmy = new person;
  14.       $jimmy->set_name("Nick Giggings");
  15.  
  16.             $number->set_pinn_number("345678");
  17.  
  18.       $stefan = new person("Stefan Mischook");
  19.  
  20. echo "Stefan's full name: " . $stefan->get_name()."<br>";
  21. echo "Nick's full name: " . $jimmy->get_name();
  22.  
  23. echo "Tell me private stuff: " . $number->get_pinn_number;
  24.  
  25. echo "hello";
  26. ?>
  27.  </body>
  28. </html> 
Now the problenm is that it doesn't out put anything and
dosn't give any error mesagges !

Thats not very user friendly !
How can I debug something ?
Jan 24 '09 #7
Dormilich
8,658 Expert Mod 8TB
@jeddiki
you probably have error messages turned off.

I've seen following: (all second code)

- line 13: missing parameter for constructor method
- line 16: object not defined
- line 23: call to undefined object, property not defined

and now the error messages I got (line numbers do not correspond)

Warning: Missing argument 1 for person::__construct(), called in /- on line 31 and defined in /- on line 9

Notice: Undefined variable: persons_name in /- on line 10

Notice: Undefined variable: number in /- on line 34

Fatal error: Call to a member function set_pinn_number() on a non-object in /- on line 34
Jan 25 '09 #8
jeddiki
290 100+
Yep,
You were right ;)

I am now displaying errors.
Jan 25 '09 #9
Dormilich
8,658 Expert Mod 8TB
instead of
Expand|Select|Wrap|Line Numbers
  1. function get_pinn_number() {
  2.       return $this->pinn_number;
  3. }     
  4. // and
  5. echo "Tell me private stuff: " . $stefan->get_pinn_number();
better write
Expand|Select|Wrap|Line Numbers
  1. function get_pinn_number() {
  2.       echo "Tell me private stuff: " . $this->pinn_number;
  3. }     
  4. // and
  5. $stefan->get_pinn_number();
this is what using objects is about. (let the class do the hard work)

regards
Jan 25 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
18
by: Bob Cummings | last post by:
Not sure if this is the correct place or not. Anyhow in school we were taught that when trying to calculate the efficiency of an algorithm to focus on something called FLOPs or Floating Point...
1
by: dadevil | last post by:
Hi guys, this is my first post around here, and here it goes I need to create an array to store info about a number "n" of persons, so i create this structure ( each person have one name and one...
1
by: AMDRIT | last post by:
Occasionally I try my hand at a simple data storage engine. Today I ran across an article on the web http://msdn2.microsoft.com/en-us/library/aa289151(vs.71).aspx, and it got me thinking again. ...
4
by: mountain.dog | last post by:
Hi There, I'm trying to understand inheriting properties object and I'm not fully understanding the ways to accomplish this. For example, I have a PHP array that is generate in "class a". I...
17
by: john | last post by:
All: I'm a long-time developer, new to PHP.... Is there an idiom used in PHP to construct SQL statments from $_POST data? I would guess that in many applications, the data read from $_POST...
7
Curtis Rutland
by: Curtis Rutland | last post by:
Building A Silverlight (2.0) Multi-File Uploader All source code is C#. VB.NET source is coming soon. Note: This project requires Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1 and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.