Solved

Mysql PHP repeat region format help

Posted on 2010-09-20
15
674 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

729 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