Solved

Mysql PHP repeat region format help

Posted on 2010-09-20
15
662 Views
Last Modified: 2013-12-12
I need a PHP repeat region for the code below. I already have the repeat region set up, but I need some additional code added.

I need to replace each line under 'flipping.Book.contents' with a field from my database called 'name'.  The number to the right of the echo show below is a page number, which I  need to increase by two with each loop.

The last line of the loop should NOT contain a comma at the end, as seen in the example below.

Any ideas?


flippingBook.contents = [
	[ "Select Publication", 1 ],

	[ "D Home - Mar-Apr 2009", 2 ],

	[ "D Home - Feb 2009", 4 ]
];

Open in new window

// THE WAY THE JAVASCRIPT CODE LOOKS NOW

flippingBook.contents = [
	<?php do { ?>

        [ "<?php echo $row_projects['name']; ?>", 1 ],

        <?php } while ($row_projects = mysql_fetch_assoc($projects)); ?>
];


// THE WAY IT NEEDS TO LOOK AFTER PROCESSING PHP

flippingBook.contents = [
	[ "Name 1", 1 ],

	[ "Name 2", 2 ],

	[ "Name 3", 4 ],
        
        [ "Name 4", 6 ],
         
        [ "Name 5", 8 ],
        
        [ "Name 6", 10 ],
        
        [ "Name 7", 12 ]
];

Open in new window

0
Comment
Question by:James_Avery
  • 8
  • 7
15 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33718233
Where are you getting the number after the name form?
0
 

Author Comment

by:James_Avery
ID: 33718494
The number doesn't come from any source. It should be automatically generated by the PHP script by simply added 2 to whatever the number in the previous loop was.

It should start with '2', then '4', '6' '8' etc... does that make sense?


0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 33718614
That does but you started with '1' in your example.  If I understand, this should do what you want.
flippingBook.contents = [
	<?php 
        $count = 0;
        while ($row_projects = mysql_fetch_assoc($projects)) { 
        $count = $count+2; ?>

        [ "<?php echo $row_projects['name']; ?>", <?php echo $count; ?> ],

        <?php } ; ?>
];

Open in new window

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:James_Avery
ID: 33718954
hmmm, this doesn't seem to be creating a repeat region.  When run, it just comes up empty.  I am not seeing any errors associated with that piece of mysql code either.

Any ideas?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33719080
Was the code you posted displaying anything?  Are you opening the database and getting the table info first?
0
 

Author Comment

by:James_Avery
ID: 33719285
Below is my code that I am working with.  For some reason anything following my first repeat region is coming up blank.  Is it because I have another repeat region?
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$query_projects = "SELECT * FROM projects WHERE activeflag = '2' AND layout = '1' ORDER BY dateadded DESC";
$projects = mysql_query($query_projects, $con) or die(mysql_error());
$row_projects = mysql_fetch_assoc($projects);
$totalRows_projects = mysql_num_rows($projects);
?>
	
flippingBook.pages = [

	"../../../images/press/print-ads/d-home-2009-03-04/02.jpg",
	<?php do { ?>
    "../../../images<?php echo $row_projects['imagepath']; ?>hero_a.jpg",    
    "../../../images<?php echo $row_projects['imagepath']; ?>hero_b.jpg",  
    <?php } while ($row_projects = mysql_fetch_assoc($projects)); ?>
	"../../../images/press/print-ads/d-home-2009-03-04/02.jpg"
    ];


flippingBook.contents = [
	<?php 
        $count = 0;
        while ($row_projects = mysql_fetch_assoc($projects)) { 
        $count = $count+2; ?>

        [ "<?php echo $row_projects['name']; ?>", <?php echo $count; ?> ],

        <?php } ; ?>
	[ "D Home - Feb 2009", 4 ]
];

Open in new window

0
 

Author Comment

by:James_Avery
ID: 33719318
I establish the connection to the DB above this code. My first repeat region works great, but the set is only printing my javascript and not the info from the database, as if the PHP is outputting nothing.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33719446
That's because you've already 'used' the info from that database in the previous section.  You can either run the query again or store the info you get into an array which you can run thru twice.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33719489
"$row_projects = mysql_fetch_assoc($projects)" increment an array pointer each time you call it.  "mysql_data_seek($projects, 0)" might reset the pointer so you can go thru the data again.  I've never used it myself so I'm not sure.
0
 

Author Comment

by:James_Avery
ID: 33719534
Oh, how silly of me.  After modifying the code by adding the query again, your solution worked.  Thanks!
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33719610
You're welcome.  It's hard to review our own code at times!
0
 

Author Comment

by:James_Avery
ID: 33720058
One last question.. I noticed your code retains the comma in the last loop of the repeat region.  On the last line, can we automatically remove the comma at the end before the  ]; ?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33720393
I think this will do it.
flippingBook.contents = [
	<?php 
        $count = 0;
        while ($row_projects = mysql_fetch_assoc($projects)) { 
        if($count) echo ",";
        $count = $count+2; ?>

        [ "<?php echo $row_projects['name']; ?>", <?php echo $count; ?> ]

        <?php } ; ?>
];

Open in new window

0
 

Author Comment

by:James_Avery
ID: 33728830
I just noticed that when using your code, I am losing the first record in the recordset.  The first record to appear is actually the second record in the table, not the first record.  The recordset I am using above shows the correct first record, but your recordset I added jumps to the 2nd record and starts from there.

Any idea why your code is shaving off the first record in the recordset and starting with the 2nd?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33728923
No.  The display $count should start with '2' because that's what you asked for.  The row should be the first row.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question